我正在尝试根据从列表框中选择的父节点的名称来获取子节点属性。
问题出在Xpath字符串" // Class [@Name ='" + curItem +"'] / Entity"中。当我用属性名称(ECMInstruction)替换变量时,它会正确显示,但是变量不会显示。
这是XML文件
<?xml version="1.0" encoding="utf-8"?>
<Root>
<Class Name="ECMInstruction" Style="Top">
<Entity Id="1" Name="DocumentInformation" />
</Class>
<Class Name="dfgfggfdg" Style="Top">
<Entity Id="1" Name="dfgfgfdgd" />
</Class>
private void dcLisT_SelectedIndexChanged(object sender, EventArgs e)
{
//EntityList.Items.Clear();
XmlDocument document = new XmlDocument();
document.Load("sample.xml");
String curItem = dcList.SelectedItem.ToString();
XmlNodeList nodes = document.DocumentElement.SelectNodes("//Class[@Name='"+curItem+"']/Entity");
foreach(XmlNode x in nodes){
String EntityID=(x.Attributes["Id"].Value);
String EntityName = (x.Attributes["Name"].Value);
EntityList.Items.Add("Entity ID #"+EntityID+" "+EntityName);
}
}