Append Node to an XML

  • Thread starter Thread starter Freddy Coal
  • Start date Start date
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
 
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
 
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")
 
Back
Top