Read XML document with LINQ TO XML -


i have little xml placed string, called mycontent:

 <people xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"     xmlns:xsd="http://www.w3.org/2001/xmlschema">      <person id="1" name="name1" />      <person id="2" name="name2" />      (....)      <person id="13" name="name13" />      <person id="14" name="name14" />  </people> 

and in c# have stored previous xml content in string variable below:

        private string mycontent = string.empty +         "<people xmlns:xsi=\"http://www.w3.org/2001/xmlschema-instance\" xmlns:xsd=\"http://www.w3.org/2001/xmlschema\">" +         "<person id=\"1\" name=\"name1\" />" +         "<person id=\"2\" name=\"name2\" />" +         (...)         "<person id=\"13\" name=\"name13\" />" +         "<person id=\"14\" name=\"name14\" />" +         "</people>"; 

and load below:

 xdocument contentxml = xdocument.parse(mycontent); 

then iterate on them:

 ienumerable<xelement> people = contentxml.elements();  foreach (xelement person in people)  {      var idperson = person.element("person").attribute("id").value;      var name = person.element("person").attribute("name").value      // print person ....  } 

the problem obtain first person, not rest. says people has 1 element , should have 14.

any ideas?

the problem asking elements() document root. there 1 element @ point, people.

what want like:

var people = contentxml.element("people").elements() 

therefore, loop like:

ienumerable<xelement> people = contentxml.element("people").elements(); foreach ( xelement person in people ) {     var idperson = person.attribute( "id" ).value;     var name = person.attribute( "name" ).value;     // print person .... } 

this iterate on each of person elements intend.


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -