Append to XML?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

I use VS 2005.
How would I append XML elements to the end of an existing XML file
programmatically?
 
Amjad said:
I use VS 2005.
How would I append XML elements to the end of an existing XML file
programmatically?

Take a look at the 'XmlDocument' class and other classes in the 'System.Xml'
namespace.
 
:
: Hi,
:
: I use VS 2005.
: How would I append XML elements to the end of an existing XML file
: programmatically?


Assume you have the following xml document in a file named Tree.xml:

<Root>
<Branch>
<Leaf/>
<Leaf/>
</Branch>
<Branch>
<Leaf/>
</Branch>
</Root>

And let's assume you want to add a <Graft /> node to this tree so that the
end result looks like this:

<Root>
<Branch>
<Leaf/>
<Leaf/>
</Branch>
<Branch>
<Leaf/>
</Branch>
<Graft/>
</Root>


Here is one approach you can take:

Imports System.XML

'...

Dim TreeXml As XMLDocument
Dim graftNode As XMLNode

TreeXml = New XMLDocument
TreeXml.Load("Tree.xml")

graftNode = TreeXml.CreateElement("Graft")
TreeXml.selectSingleNode("/Root").AppendChild(graftNode)

Console.WriteLine(TreeXml.InnerXMl)

This will generate the following output:

<Root>
<Branch>
<Leaf />
<Leaf />
</Branch>
<Branch>
<Leaf />
</Branch>
<Graft />
</Root>

HTH


Ralf
 
Back
Top