【发布时间】:2016-02-19 01:08:02
【问题描述】:
我有一个非常大 (~1GB) 的 XML 文件。我需要解析它,找到特定节点,更改这些节点中的数据,然后将其全部写入一个新的 XML 文件。这就是问题所在——有很多我不关心的元素——我什至不知道它们都是什么——但它们也需要被复制。
This SO post 建议我使用 XmlReader,这样我就不必将整个输入文件加载到内存中。该问题有this 答案,建议使用ReadToDescendant 方法。这几乎可以满足我的需要,但问题是我在“读取”的节点之前丢失了所有 XML。不知何故,我需要将我刚刚读过的所有内容复制到新文件中。我不在乎那里有什么,只需要逐字复制。
This SO 帖子可以工作(还有其他几个类似的帖子),除了它使用XmlDocument,如果我没记错的话,它将首先将整个内容加载到内存中。虽然这对于小文件来说很好,但我想在这里避免这种情况。
对于您的视觉类型,这是我想要做的一个想法:
<root>
<SomeNodeUndefinedAtDesignTime>
<ThisNodeHasSubNodes>
<WhichHasSubNodes_Etc/>
</ThisNodeHasSubNodes>
</SomeNodeUndefinedAtDesignTime>
<AnotherUndefinedNode>
<!--Similar to the first, who knows what all is in here-->
</AnotherUndefinedNode>
<!-- There may be dozens or even hundreds of these -->
<ANodeIAmInterestedIn>
Old data to be replaced
</ANodeIAmInterestedIn>
<ANodeIAmInterestedIn>
More data to be replaced
</ANodeIAmInterestedIn>
<YetAnotherUndefinedNode>
<!-- stuff -->
</YetAnotherUndefinedNode>
</root>
我想接受那个输入然后输出这个:
<root>
<SomeNodeUndefinedAtDesignTime>
<ThisNodeHasSubNodes>
<WhichHasSubNodes_Etc/>
</ThisNodeHasSubNodes>
</SomeNodeUndefinedAtDesignTime>
<AnotherUndefinedNode>
<!--Similar to the first, who knows what all is in here-->
</AnotherUndefinedNode>
<!-- There may be dozens or even hundreds of these -->
<ANodeIAmInterestedIn>
Here is my new data
</ANodeIAmInterestedIn>
<ANodeIAmInterestedIn>
Here is more new data
</ANodeIAmInterestedIn>
<YetAnotherUndefinedNode>
<!-- stuff -->
</YetAnotherUndefinedNode>
</root>
有没有办法
- 将文件作为流读取,这样我就不必一次将整个内容加载到内存中
- 复制设计时未定义的元素
- 更改特定元素中的数据
- 将结果写入新文件
【问题讨论】: