从xml文件填充Datagrid

时间:2018-07-02 09:04:38

标签: c# xml datagridview

我已经查看了有关该主题的许多答案,但找不到我的问题的答案,因此,如果我在其他地方错过了此答案,请提前道歉。

我想将特定对象ID的属性添加到数据网格。不幸的是,我开发的例程将所有属性添加到了数据网格中,而不仅是所选对象的属性。

我具有以下xml文件结构:

<?xml version="1.0" encoding="UTF-8"?>
<project>
  <configuration>
    <general>
      <verbose>True</verbose>
    </general>
    <architecture>
      <site reference="Demo Site Reference" type="WTP" id="0001" />
      <rtu dnp="77" ip="10.10.10.77" type="Schneider Electric SCADAPack 442" />
      <radio ip="10.10.10.76" />
      <hmi ip="10.10.10.75" />
    </architecture>
  </configuration>
  <database>
    <object id="0" name="object 0" description="Entry Description 0" date="22.06.2018 00:00:00" archestra="Export">
      <attributes>
        <attribute id="0" name="Attribute 0" description="Attribute 0 Description" note="Attribute 0 Note" />
        <attribute id="1" name="Attribute 1" description="Attribute 1 Description" note="Attribute 1 Note" />
      </attributes>
    </object>
    <object id="1" name="object 1" description="Entry Description 1" date="22.06.2018 00:00:00" archestra="Export">
      <attributes>
        <attribute id="0" name="Attribute 0" description="Attribute 0 Description" note="Attribute 0 Note" />
        <attribute id="1" name="Attribute 1" description="Attribute 1 Description" note="Attribute 1 Note" />
        <attribute id="2" name="Attribute 2" description="Attribute 2 Description" note="Attribute 2 Note" />
        <attribute id="3" name="Attribute 3" description="Attribute 3 Description" note="Attribute 3 Note" />
      </attributes>
    </object>
    <object id="2" name="object 2" description="Entry Description 2" date="22.06.2018 00:00:00" archestra="Export">
      <attributes>
        <attribute id="0" name="Attribute 0" description="Attribute 0 Description" note="Attribute 0 Note" />
        <attribute id="1" name="Attribute 1" description="Attribute 1 Description" note="Attribute 1 Note" />
        <attribute id="2" name="Attribute 2" description="Attribute 2 Description" note="Attribute 2 Note" />
      </attributes>
    </object>
  </database>
</project>

以下例程添加了所有属性:

    public static DataTable PopulateGrid(string file, string id)
    {
        //DataTable that will hold the found results
        DataTable results = new DataTable("SearchResults");
        //DataRow (used later)
        DataRow row = null;

        results.Columns.Add("id", typeof(string));
        results.Columns.Add("name", typeof(string));
        results.Columns.Add("description", typeof(string));

        XmlDocument xmldocument = new XmlDocument();
        xmldocument.Load(file);

        //** Change This **
        string query = "/project/database/object";
        string query2 = "/project/database/object/attributes/attribute";

        //now we loop through the list
        foreach (XmlNode xmlnode in xmldocument.SelectNodes(query))
        {
            if (xmlnode.Attributes["id"].Value == id)
            {
                foreach (XmlNode xmlnode2 in xmldocument.SelectNodes(query2))
                {
                    row = results.NewRow();

                    row[0] = xmlnode2.Attributes["id"].Value;
                    row[1] = xmlnode2.Attributes["name"].Value;
                    row[2] = xmlnode2.Attributes["description"].Value;

                    results.Rows.Add(row);
                }
            }
        }

        //now return the DataTable
        return results;
    }

任何解决此问题的帮助将不胜感激。

亲切问候

3 个答案:

答案 0 :(得分:2)

您必须从已经找到的节点而不是整个文档中获取属性。

string query2 = "./attributes/attribute"; // path from current node


// use xmlnode instead of xmldocument
foreach (XmlNode xmlnode2 in xmlnode.SelectNodes(query2))

答案 1 :(得分:1)

在第二个for循环中,您调用

xmldocument.SelectNodes(query2)

从文档中选择与query2匹配的所有节点。

您可以将代码更改为:

        string query = "/project/database/object['" + id + "']";
        string query2 = "attributes/attribute";

        var obj = xmldocument.SelectSingleNode(query);

        foreach (XmlNode xmlnode2 in obj.SelectNodes(query2))
        {
            row = results.NewRow();

            row[0] = xmlnode2.Attributes["id"].Value;
            row[1] = xmlnode2.Attributes["name"].Value;
            row[2] = xmlnode2.Attributes["description"].Value;

            results.Rows.Add(row);
        }

答案 2 :(得分:1)

我认为如果您使用Linq To XML会容易得多。即:

public static IEnumerable<MyData> PopulateGrid(string file, string id)
{
    var xml = XElement.Load(file);
    var result = xml.Element("database").Elements("object")
    .SelectMany(o => o.Element("attributes").Elements("attribute"))
    .Where(x => (string)x.Attribute("id") == id)
    .Select(a => new MyData
    {
        Id = (string)a.Attribute("id"),
        Name = (string)a.Attribute("name"),
        Description = (string)a.Attribute("description")
    });

    return result;
}

public class MyData
{
    public string Id { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
}