Vimal Raj
Vimal Raj

Reputation: 1038

How to Update an xml file without loading the entire file in memory

How can we update an xml file without loading it entirely into memory. In the following code I want to navigate through each parent node note and update the value of the to node. How can we achieve this using C#. I have to update the to field based on some other calculation that I have in code.

<note>
<from>Jani</from>
<to>Tove</to>
</note>
<note>
<from>John</from>
<to>Doe</to>
</note>

Upvotes: 2

Views: 2736

Answers (2)

Damith
Damith

Reputation: 63065

you can use XmlReader and XmlWriter. here are some examples.

http://dotnetperls.com/xmlreader, http://dotnetperls.com/xmlwriter

Upvotes: 1

Oded
Oded

Reputation: 499002

You can use a streaming approach with XmlReader and XmlWriter, where you simply write out everything from the reader apart from the portions you need to change.

Scott Hanselman has a blog post using this technique.

Upvotes: 7

Related Questions