[英]XML parsing C# using LINQ
How do you parse this kind of XML file with LINQ? 您如何使用LINQ解析这种XML文件?
<houses>
<house nbr="146" city="Linköping" owner="john"/>
<house nbr="134" city="Norrköping" owner="wayne"/>
<house nbr="146" city="Köping" owner="steffe"/>
</houses>
All examples I can find only describe how to parse when each element has a value. 我可以找到的所有示例仅描述了每个元素都有值时如何解析。
If this was the case I would have done it like this: 如果是这种情况,我会这样做:
var houses = from house in xmlDoc.Descendants("house")
select new RowData
{
number = spec.Element("nbr").Value,
city = spec.Element("city").Value,
owner = spec.Element("owner").Value,
};
return houses ;
But this xml file is not formatted that way. 但是,此xml文件的格式不是这种格式。
Try this: 尝试这个:
var houses = from house in document.Descendants("house")
select new RowData
{
number = (int)house.Attribute("nbr"),
city = (string)house.Attribute("city"),
owner = (string)house.Attribute("owner")
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.