Read xml using Linq to XML

vattsluth

New Member
Please review code and advice what change needs to be done in order to get the expected output. The select code is pseudo.xml:\[code\]<?xml version='1.0' encoding='Cp1252' ?><RESULTS> <ROW> <COLUMN NAME="ID"><![CDATA[1234]]></COLUMN> <COLUMN NAME="FName"><![CDATA[Joe]]></COLUMN> <COLUMN NAME="LName"><![CDATA[Doe]]></COLUMN> </ROW> <ROW> <COLUMN NAME="ID"><![CDATA[5678]]></COLUMN> <COLUMN NAME="FName"><![CDATA[Jill]]></COLUMN> <COLUMN NAME="LName"><![CDATA[Martin]]></COLUMN> </ROW></RESULTS>\[/code\]code:\[code\]static void Main(string[] args){ XDocument doc = XDocument.Load("C:\\test.xml"); var query = from headrow in doc.Descendants("ROW") from COLUMN in headrow.Descendants("COLUMN") select new { ID = COLUMN.Element where (string)COLUMN.Attribute== "ID", FName = COLUMN.Element where (string)COLUMN.Attribute== "FName", LName = COLUMN.Element where (string)COLUMN.Attribute== "LName" }; foreach (var result in query) { Console.WriteLine(result); } }\[/code\]Exprected output
1234 Joe Doe
5678 Jill Martin
thanks in advance
 
Back
Top