linq-xml Query not returning expected results -
i have following xml file consists of html tags, shown below:
<?xml version="1.0" encoding="utf-8"?> <html> <head> <title> title1 </title> </head> <body> <fragment id="heading1"> <h1> heading 1 </h1> </fragment> <fragment id="heading2"> <h2> heading 2 </h2> </fragment> <fragment id="paragraph1"> <p> paragraph 1 </p> </fragment> </body> </html>
i trying extract fragment ids , display them using linq-xml. query shown below:
xdocument xelement = xdocument.load("path\\to\\xmlfile"); var name = nm in xelement.descendants("body") select nm.element("fragment").attribute("id").value; console.writeline(name);
the output query returns is:
heading1
but want is:
heading1 heading2 paragraph1
what doing wrong? kindly advice.
thank you
i have tested , working fine!!
xdocument po = xdocument.load(@"xmlfile1.xml"); ienumerable<string> names = id in po.descendants("fragment").attributes("id") select id.value; string str = string.empty; foreach (var el in names) { str += el; } system.console.writeline(str); console.readkey();
Comments
Post a Comment