如何从xmlnodelist向动态创建的组合框添加项目。
<Root>
<Class Name="ECMInstruction" Style="Top">
<Entity Id="1" Name="DocumentInformation" />
<Entity Id="2" Name="CustomerInformation" />
<Property Id="1" Name="DocumentTitle">
</Property>
<Property Id="2" Name="DateCreated">
<Lists>
<ListName>ws_Users</ListName>
<ListName>dfdfdfd</ListName>
</Lists>
</Property>
<Property Id="3" Name="Deadline">
</Property>
</Class>
<Class Name="AlphaCertificationsIndividual" Style="Top">
<Entity Id="1" Name="DocumentInformation" />
<Property Id="1" Name="DocumentTitle">
</Property>
<Property Id="2" Name="DateCreated">
<Lists>
<ListName>ws_Users</ListName>
<ListName>dfdfdfd</ListName>
</Lists>
</Property>
<Property Id="3" Name="Deadline">
</Property>
</Class>
</Root>
迭代xml文件以获取所有属性并相应地创建其标签和文本框。当它到达属性实体时,我想要放置来自&#34;实体&#34;的id属性的所有值。 XmlNodeList中。
XmlNodeList attributes = document.DocumentElement.SelectNodes("//Class[@Name='" + classname + "']/Property[@Id='" + id + "']/attribute::*");
XmlNodeList entities = document.DocumentElement.SelectNodes("//Class[@Name='" + classname + "']/Entity");
table1.Controls.Add(new Label() { Text = attributes[x].Name, AutoSize = true });
table1.Controls.Add(new ComboBox() { Name = attributes[x].Name, SelectedText = attributes[x].Value,Items = { entities[x].Value }, AutoSize = true });
编译器给我空引用
答案 0 :(得分:0)
您的代码不完整,但我认为您写了
foreach (XmlNode x in attributes) { ... }
或类似的。这不包括实体元素。
你应该尝试:
List<string> items = new List<string>();
foreach (XmlNode y in x.ParentNode.SelectNodes("Entity"))
{
items.Add(y.Value);
}
table1.Controls.Add(new ComboBox() { Name = attributes[x].Name, SelectedText = attributes[x].Value,Items = items, AutoSize = true });
或者如果您更喜欢和熟悉Linq,您可以写:
x.ParentNode.SelectNodes("Entity").Cast<XmlNode>().AsQueryable().Select(n => n.Value);