请查看代码并告知需要进行哪些更改才能获得预期的输出。选择代码是伪的。
的xml:
<?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>
代码:
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);
}
}
Exprected output
1234 Joe Doe
5678吉尔马丁
提前致谢
答案 0 :(得分:1)
查看以下内容是否有帮助:
var query = from headrow in doc.Descendants("ROW")
select new
{
ID = headrow.Elements("COLUMN").First(c => c.Attribute("NAME").Value == "ID").Value,
FName = headrow.Elements("COLUMN").First (c => c.Attribute("NAME").Value == "FName").Value,
LName = headrow.Elements("COLUMN").First (c => c.Attribute("NAME").Value == "LName").Value
};
foreach (var result in query)
{
Console.WriteLine("{0} {1} {2}", result.ID, result.FName, result.LName);
}