LINQ to XML提取嵌套元素 [英] LINQ to XML extract nested elements
本文介绍了LINQ to XML提取嵌套元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是LINQ和XML解析的新手,而是C#编程的新手.对于以下XML结构,我正在尝试提取嵌套元素:
I'm new to LINQ and XML parsing and rather new to C# programming. For the following XML structure, I'm trying to extract the nested elements:
<persons>
<person>
<personNumber>2</personNumber>
<info>free text</info>
<addresses>
<address>
<city>XXX</city>
<location>1</location>
</address>
<address>
<city>YYY</city>
<location>2</location>
</address>
</addresses>
</person>
<person>
<personNumber>3</personNumber>
<info>free text</info>
<addresses>
<address>
<city>XXX</city>
<location>1</location>
</address>
<address>
<city>YYY</city>
<location>2</location>
</address>
</addresses>
</person>
</persons>
我希望能够为personNumber = 2的所有人获取所有城市和位置!
I want to be able to fetch all the city and location for all persons with personNumber = 2!
推荐答案
您可以使用linq这样操作:
You can do it this way using linq:
var result = from p in xmlDoc.Descendants("person")
from a in p.Descendants("address")
where p.Element("personNumber").Value == "2"
select new
{
City = a.Element("city").Value,
Location = a.Element("location").Value
};
这篇关于LINQ to XML提取嵌套元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文