append text to a xml file

E

Eranga

I want to append text to element3 and element 4 of a .xml file as
following. The values in first two elements should not be altered and
only the last 2 sholud be changed.

<root>
<element1>aaaa</element1>
<element2>cccc</element2>
<element3>bbbb</element3>
<element4>dddd</element4>
</root>

But using textwriter I could not do this. It gets replaced with the last
two elements . Could you pleas tell me how I can do this??
Thanks a lot
 
T

Tim Haughton

Eranga said:
I want to append text to element3 and element 4 of a .xml file as
following. The values in first two elements should not be altered and
only the last 2 sholud be changed.

<root>
<element1>aaaa</element1>
<element2>cccc</element2>
<element3>bbbb</element3>
<element4>dddd</element4>
</root>

Is there anything stopping you reading the file into memory, appending the
new children, then saving it back to disk?

--
Regards,

Tim Haughton

Agitek
http://agitek.co.uk
http://blogitek.com/timhaughton
 
G

Guest

Load the XML file into an XmlDocument object and then iterate over the
elements until you find the one you want to modify OR call
GetElementsByTagName passing in the name of the element and then set the
value by using the InnerText property of the returned XmlNode.

After this, just save the file back.

Brian Delahunty
Ireland
 

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