如何在不加载在内存中的整个文件更新XML文件 [英] How to Update an xml file without loading the entire file in memory

查看:204
本文介绍了如何在不加载在内存中的整个文件更新XML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在不完全加载到内存中,我们可以更新XML文件。在下面的code我想浏览每个父节点说明和更新的值到节点。我们如何才能做到这一点使用C#。我必须更新基于一些其他的计算,我在code到现场。

 <注意>
<从>贾尼< /从>
&所述;至大于托弗&所述; /到大于
< /笔记>
<注>
<从>约翰< /从>
&所述;至大于Doe的所述; /到大于
< /笔记>
 

解决方案

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

部分

斯科特Hanselman有一个<一个href="http://www.hanselman.com/blog/StrippingOutEmptyXmlElementsInAPerformantWayAndTheBusFactor.aspx"相对=nofollow>博客发布使用这种技术。

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.

这篇关于如何在不加载在内存中的整个文件更新XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆