[英]Parsing a file containing xml elements in C#
I have a big xml file which is composed of multiple xml files.我有一个大的 xml 文件,它由多个 xml 文件组成。 The file structure is like this.
文件结构是这样的。
<xml1>
</xml1>
<xml2>
</xml2>
<xml3>
</xml3>
.
.
<xmln>
</xmln>
I want to process each XML file by a LINQ statement but I am not sure how to extract each XML element individually from the file and continue the iteration until the end.我想通过 LINQ 语句处理每个 XML 文件,但我不确定如何从文件中单独提取每个 XML 元素并继续迭代直到结束。 I figured experts here would be able to give me the nudge I am looking for.
我认为这里的专家能够给我我正在寻找的轻推。 My requirement is to extract the individual elements so i could load into a XDoc element for further processing and continue on through the subsequent elements.
我的要求是提取单个元素,以便我可以加载到 XDoc 元素中以进行进一步处理并继续处理后续元素。 Any help on this would be greatly appreciated.
对此的任何帮助将不胜感激。
Thanks for reading!!谢谢阅读!!
Assuming each element is a valid XML, you can wrap your document in a top-level tag and then load it just fine:假设每个元素都是有效的 XML,您可以将文档包装在顶级标签中,然后正常加载:
<wrap>
<xml1>
</xml1>
<xml2>
</xml2>
</wrap>
If this is an error log with individual well formed xml elements, you probably don't want to load it as an XMLDocument
.如果这是带有单个格式良好的 xml 元素的错误日志,您可能不想将其作为
XMLDocument
加载。 A better option is to use an XPathDocument
created with an XmlReader
where the XmlReaderSettings
specify ConformanceLevel.Fragment
.更好的选择是使用通过
XmlReader
创建的XPathDocument
,其中XmlReaderSettings
指定ConformanceLevel.Fragment
。 Then you can use an XPathNavigator
to access your elements and attributes as needed.然后,您可以根据需要使用
XPathNavigator
访问您的元素和属性。 See the third answer in this forum post for sample code.有关示例代码,请参阅此论坛帖子中的第三个答案。
...late by just a year and then some. ...晚了一年,然后是一些。 :)
:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.