Append Node to an XML

F

Freddy Coal

Hi, I would like append a new node with elements in a existing XML file, how
make that?.

For example:

<?xml version="1.0" encoding="utf-8" ?>
- <Configuracion>
- <Estaciones>
<Capa_Trabajo>C:\Temp\point.shp</Capa_Trabajo>
<Estados>C:\Temp\WGS84_region.shp</Estados>
<IndexA>1</IndexA>
<Date>Date=21/03/2007 09:55:52</Date>
</Estaciones>
</Configuracion>

I would like append a node, how make this node?

<Links>
<SiteA> ontario </SiteA>
<SiteB> Canton </SiteB>
</Links>

And my final XML is:

<?xml version="1.0" encoding="utf-8" ?>
- <Configuracion>
- <Estaciones>
<Capa_Trabajo>C:\Temp\point.shp</Capa_Trabajo>
<Estados>C:\Temp\WGS84_region.shp</Estados>
<IndexA>1</IndexA>
<Date>Date=21/03/2007 09:55:52</Date>
</Estaciones>
<Links>
<SiteA> ontario </SiteA>
<SiteB> Canton </SiteB>
</Links>
</Configuracion>

Thanks in advance.

Freddy Coal
 
R

Rick

I'm not sure what net component you are using in your program for the
existing xml, however you can lookup XmlFragment in the help. You create an
XmlFragment from the source document and then AppendChile (or something
similar) to add it to the document.

Rick
 
M

Martin Honnen

Freddy said:
I would like append a node, how make this node?

<Links>
<SiteA> ontario </SiteA>
<SiteB> Canton </SiteB>
</Links>

And my final XML is:

<?xml version="1.0" encoding="utf-8" ?>
- <Configuracion>
- <Estaciones>
<Capa_Trabajo>C:\Temp\point.shp</Capa_Trabajo>
<Estados>C:\Temp\WGS84_region.shp</Estados>
<IndexA>1</IndexA>
<Date>Date=21/03/2007 09:55:52</Date>
</Estaciones>
<Links>
<SiteA> ontario </SiteA>
<SiteB> Canton </SiteB>
</Links>
</Configuracion>

Use the DOM implementation in the .NET framework, that is
System.Xml.XmlDocument:

Dim XmlDoc As XmlDocument = New XmlDocument()
XmlDoc.Load("file.xml")

Dim Links As XmlElement = XmlDoc.CreateElement("Links")
Dim SiteA As XmlElement = XmlDoc.CreateElement("SiteA")
SiteA.InnerText = "ontario"
Dim SiteB As XmlElement = XmlDoc.CreateElement("SiteB")
SiteB.InnerText = "Canton"
Links.AppendChild(SiteA)
Links.AppendChild(SiteB)

XmlDoc.DocumentElement.AppendChild(Links)

XmlDoc.Save("file.xml")
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top