Venki
Venki

Reputation: 1459

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!!

Upvotes: 0

Views: 251

Answers (3)

Sarin
Sarin

Reputation: 1285

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. :)

Upvotes: 0

WEFX
WEFX

Reputation: 8552

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.

Upvotes: 0

Anton Gogolev
Anton Gogolev

Reputation: 115741

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>

Upvotes: 2

Related Questions