:
: 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