[英]How to read data from xml file using linq to xml?
Trying to read this xml using Linq to XML 尝试使用Linq to XML读取此xml
<Root>
<shelves>
<bookNumber>12</bookNumber>
<coverType unit="H">soft</coverType>
<pages>100</pages>
<Weight units="lb">1.2</Weight>
<chapter sample="1">example 1</<chapter>
<chapter sample="2">example 2</<chapter>
<chapter sample="3">example 3</<chapter>
<chapter sample="4">example 4</<chapter>
<chapter sample="5">example 5</<chapter>
<chapter sample="6">example 6</<chapter>
<chapter sample="7">example 7</<chapter>
<chapter .................</chapter>
<chapter .................</chapter>
<chapter .................</chapter>
<chapter .................</chapter>
<chapter .................</chapter>
..............
</shelves>
</Root>
Thats the code i am trying with:-. 这就是我正在尝试的代码: - 。 But How will read values all the elements 'Chapter'?
但是如何读取所有元素'Chapter'的值?
var book = from b in xml.Root.Elements("shelves")
select b;
foreach (var s in book)
{
booknumber = s.Element("bookNumber").Value,
covertype = s.Element("bookNumber").Value,
coverTypeUnit = s.Element("bookNumber").Attribute("unit").Value,
...........
chapter = s.Element("bookNumber").Value ????
}
You may find these links usefull: 您可能会发现这些链接很有用:
var values = s.Elements("chapter").Select(n => n.Value).ToArray();
Furthermore, you're reading from the same element (booknumber) over and over. 此外,你一遍又一遍地阅读相同的元素(书本号)。 You might want to check your code.
您可能想检查您的代码。
EDIT: to also yield the attribute: 编辑:也产生属性:
s.Elements("chapter").
Select(n => new {Topic = n.Attribute("topic").Value, Value = n.Value}).
ToArray();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.