简体   繁体   中英

Parsing a file containing xml elements in C#

I have a big xml file which is composed of multiple xml files. 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. 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. 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:

<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 . A better option is to use an XPathDocument created with an XmlReader where the XmlReaderSettings specify ConformanceLevel.Fragment . Then you can use an XPathNavigator to access your elements and attributes as needed. See the third answer in this forum post for sample code.

...late by just a year and then some. :)

You can use the System.Xml reference, which includes a lot of built-in functionality, and it allows you to declare an XmlTextReader. More info here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM