次のコードはBuilding Phone
を出力しますが、出力はuxPhone
ではありません。
1)Property
子孫のコレクションを取得する必要がありますか?
2)これは非常に冗長なようですが、これを行うのに短い形式がありますか?代わりに、単一のいずれかを選択control.Element("Property")
のLinq to Xmlは最初の子孫値のみを出力しています
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); }
}
}
MostafizとGiladありがとうございました。私はあなたのうちの1人に答え、もう1人にはアップヴォートをマークします。 Giladさんのコードは、6.0の言語機能のために私のために機能しませんでしたか?もしあなたが6.0の機能を説明するならば、あなたはそれを4.5で再加工することができます。 – bkolluru