0

I have an XElement and I need to add more elements to it from a string. The string contains multiple xml elements in it that I need to parse. I have tried both XDocument.Load and XElement.Parse but get errors. The contents of the file are as follows:

<menu id="a">
  <menuItem keyID="0">
    <command>test</command>
  </menuItem>
  <menuItem keyID="0">
    <command>test</command>
  </menuItem>     
</menu>
<menu id="b">
  <menuItem keyID="0">
    <command>test</command>
  </menuItem>
  <menuItem keyID="0">
    <command>test</command>
  </menuItem>     
</menu>    
<ecs>
  <areas>
    <area longitudeFrom="20.35" longitudeTo="20.37" />
    <area longitudeFrom="20.44" longitudeTo="20.46" />
    <area longitudeFrom="20.22" longitudeTo="20.25" />
  </areas>
</ecs>

What is the best way to read all the elements from this string and add them to an existing XElement ?

Shahid
  • 995
  • 3
  • 11
  • 24

1 Answers1

1

if this is the whole file it is normal XMLDocument.Load to give you errors. For a XML to be loaded it must have one root like this. If the error is in something else please post the error message

<root>
    <menu id="a">
      <menuItem keyID="0">
        <command>test</command>
      </menuItem>
      <menuItem keyID="0">
        <command>test</command>
      </menuItem>     
    </menu>
    <menu id="b">
      <menuItem keyID="0">
        <command>test</command>
      </menuItem>
      <menuItem keyID="0">
        <command>test</command>
      </menuItem>     
    </menu>    
    <ecs>
      <areas>
        <area longitudeFrom="20.35" longitudeTo="20.37" />
        <area longitudeFrom="20.44" longitudeTo="20.46" />
        <area longitudeFrom="20.22" longitudeTo="20.25" />
      </areas>
    </ecs>
</root>
SliverNinja - MSFT
  • 31,051
  • 11
  • 110
  • 173
IordanTanev
  • 6,130
  • 5
  • 40
  • 49
  • XDocument.Load complains about root element being missing I have now added a root element but now it says [System.ArgumentException] = {"Non white space characters cannot be added to content."} And actually the string containing xml is too big so I just posted a sample here. Is there anyway I can parse it with or without a root element ? – Shahid Feb 03 '10 at 15:45
  • About the new error are you sure there are no text outside the xml and the xml is valid. On the remark about reading the xml without root i don't think it is possible – IordanTanev Feb 03 '10 at 16:02