Home All Groups Group Topic Archive Search About
Author
16 Jan 2006 3:51 PM
Amjad
Hi,

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

Author
16 Jan 2006 6:09 PM
Herfried K. Wagner [MVP]
"Amjad" <Am***@discussions.microsoft.com> schrieb:
> 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.

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>
Author
17 Jan 2006 3:55 AM
_AnonCoward
"Amjad" <Am***@discussions.microsoft.com> wrote in message
news:B420CE51-C814-488D-914A-817C0DC541CF@microsoft.com...
:
: 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
--
--
----------------------------------------------------------
*             ^~^                   ^~^                  *
*          _ {~ ~}                 {~ ~} _               *
*         /_``>*<                   >*<''_\              *
*        (\--_)++)                 (++(_--/)             *
----------------------------------------------------------
There are no advanced students in Aikido - there are only
competent beginners. There are no advanced techniques -
only the correct application of basic principles.