从xmlnodelist向组合框添加项目

本文关键字:添加 项目 组合 xmlnodelist | 更新日期: 2023-09-27 18:17:29

如何从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文件以获取所有属性并相应地创建它们的标签和文本框。当它到达属性实体时,我想从"实体"XmlNodeList中放置id属性的所有值。

    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 });

编译器给我null引用

从xmlnodelist向组合框添加项目

你的代码不完整,但我认为你写了一个

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);