The following code is printing Building Phone
but not printing uxPhone
.
1) Should I be getting a collection of Property
descendants maybe?
2) This seems pretty verbose, is there a shorter form of doing this?
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); }
}
}
The use of the
Element
function incontrol.Element("Property")
returns a single element. You want instead to useElements
.A nicer way all together is to use
Descendants("Property")
(which searches recursively in your xml and returns the collection of elements of the<>
you specified) and instead ofif
statements to use awhere
clause:Instead of
control.Element("Property")
which select single one, usecontrol.Elements("Property")
which select all withProperty