Vivendi
Vivendi

Reputation: 21007

Get multiple Root elements with XDocument

I have an XML file that looks like this:

<?xml version="1.0" encoding="utf-8" ?>
<Page1>
  <item name="somefield1">111</item>
  <item name="somefield1">111</item>
</Page1>
<Page2>
  <item name="somefield1">111</item>
  <item name="somefield1">111</item>
</Page2>
<Page3>
  <item name="somefield1">111</item>
  <item name="somefield1">111</item>
</Page3>

I want to get all the Root element names. So in this case: Page1, Page2 and Page3. But the amount of elements and their name can be anything.

But i can't figure out how to get all the names of the root elements. How can i do this with Xdocument?

Upvotes: 0

Views: 1689

Answers (1)

Roy Dictus
Roy Dictus

Reputation: 33139

What you are asking for is impossible. It is incorrect XML to have more than 1 root element, so System.Xml.* classes do not support that.

What you can do is put a root element around your Page elements:

<?xml version="1.0" encoding="utf-8" ?>
<Pages>
    <Page1>
      <item name="somefield1">111</item>
      <item name="somefield1">111</item>
    </Page1>
    <Page2>
      <item name="somefield1">111</item>
      <item name="somefield1">111</item>
...
</Pages>

and then it's easy to find, right under that element, your collection of Pages.

Upvotes: 2

Related Questions