且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在不加载在内存中的整个文件更新XML文件

更新时间:2023-11-24 19:59:52

您可以使用流媒体的方式与的XmlReader 的XmlWriter ,在那里你只需写出一切从读者除了你需要改变。

部分

斯科特Hanselman有一个博客发布使用这种技术。

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>

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.