使用linq从xml中检索值

时间:2012-05-10 09:57:27

标签: linq linq-to-xml

我有这个XML数据

<Address Location="ABC">
   <Add Location="XYZ1" street="street1"  />
   <Add Location="VZC" street="street1" />
</Address>

我想找出<add> --> street <{1}}

的值street1

我试过如下,没有得到结果

var q = from res in xmlDoc.Descendants("Address")
        where res.Attribute("Location").Value == "ABC"
              && res.Element("Add").Attribute("Location").Value == "VZC"
        select new
               {
                  streetadd= res.Element("Add").Attribute("street").Value,
               };

在这种情况下,请有人建议如何检查子元素的条件。

1 个答案:

答案 0 :(得分:0)

试试这个:

        var query = from res in xmlDoc.Descendants("Address")
                                where res.Attribute("Location").Value == "ABC"
                                from child in res.Elements("Add")
                                where child.Attribute("Location").Value == "VZC"
                                select new
                                {
                                    streetadd = child.Attribute("street").Value
                                };