Linq to Xml 仅打印第一个后代值

Linq to Xml is printing only first descendant value

以下代码打印 Building Phone 但不打印 uxPhone
1) 我应该收集 Property 个后代吗?
2) 这看起来很冗长,有更简短的形式吗?

var xmlstr =
        @"<Form>
        <ControlsLayout>
        <Object type='sometype' children='Controls'>
        <Property name='ControlLabel'>BuildingPhone</Property>
        <Property name='Name'>uxPhone</Property>
        </Object>
        </ControlsLayout>
        </Form>";

XElement xelement = XElement.Parse(xmlstr);      
var controls = xelement.Descendants("Object");
foreach (var control in controls)
{
    var xElement = control.Element("Property");
    if (xElement != null)
    {
        var xAttribute = xElement.Attribute("name");
        if (xAttribute != null && xAttribute.Value == "ControlLabel")
            { Console.WriteLine(xElement.Value); }
            if (xAttribute != null && xAttribute.Value == "Name")
            { Console.WriteLine(xElement.Value); }
    }
}

而不是 control.Element("Property") which select 一个,使用 control.Elements("Property") which select all with Property

 XElement xelement = XElement.Parse(xmlstr);
 //var controls = xelement.Descendants("ControlsLayout");
 var controls = xelement.Descendants("Object");
 foreach (var control in controls)
 {
    var xElement = control.Elements("Property"); // change this line
    foreach (var element in xElement)
    {
        if (element != null)
        {
            var xAttribute = element.Attribute("name");
            if (xAttribute != null && xAttribute.Value == "ControlLabel")
            { Console.WriteLine(element.Value); }
            if (xAttribute != null && xAttribute.Value == "Name")
            { Console.WriteLine(element.Value); }
        }
    }

 }

Should I be getting a collection of Property descendants maybe?

control.Element("Property")returns单个元素中使用Element函数。你想改用 Elements.

This seems pretty verbose, is there a shorter form of doing this?

一个更好的方法是一起使用 Descendants("Property")(它在您的 xml 和 returns 中递归搜索您指定的 <> 的元素集合)和使用 where 子句代替 if 语句:

XElement xelement = XElement.Parse(xmlstr);
var result = from element in xelement.Descendants("Property")
             let attribute = element.Attribute("name")
             where (attribute != null && attribute.Value == "ControlLabel" )||
                   (attribute != null && attribute.Value == "Name" )
             select element.Value;

foreach(var item in result)
    Console.WriteLine(item);

// Building Phone
// uxPhone