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

keyboard - C++ GetAsyncKeyState alternative -

android - java.net.UnknownHostException(Unable to resolve host “URL”: No address associated with hostname) -