如何将具有不同数量节点和子节点的 XML 文件导入 C# 中的 DataTable?

How to import XML file with varying number of nodes and child nodes into DataTable in C#?

我有以下 XML 文件需要导入到 C# 中的数据表中:

<?xml version="1.0"?>
<DATA>
    <SystemID>
        <Information>
        </Information>
    </SystemID>
    <Measurement_Data>
        <Channel_01>
            <Parameter_1 Attribute1="double number" Attribute2="string" Attribute3="double number" Attribute4="string" />
            <Parameter_2 Attribute1="double number" Attribute2="string" Attribute3="double number" Attribute4="string" />
            <Parameter_3 Attribute1="double number" Attribute2="string" Attribute3="double number" Attribute4="string" />
            .
            .
            .
            <Parameter_N Attribute1="double number" Attribute2="string" Attribute3="double number" Attribute4="string" />
        </Channel_01>
        <Channel_02>
            <Parameter_A Attribute1="double number" Attribute2="string" Attribute3="double number" Attribute4="string" />
            <Parameter_B Attribute1="double number" Attribute2="string" Attribute3="double number" Attribute4="string" />
            <Parameter_C Attribute1="double number" Attribute2="string" Attribute3="double number" Attribute4="string" />
            .
            .
            .
            <Parameter_N Attribute1="double number" Attribute2="string" Attribute3="double number" Attribute4="string" />
        </Channel_02>
        .
        .
        .
        <Channel_Z>
            <Parameter_1A Attribute1="double number" Attribute2="string" Attribute3="double number" Attribute4="string" />
            <Parameter_2A Attribute1="double number" Atribute2="string" Attribute3="double number" Attribute4="string" />
            <Parameter_3A Attribute1="double number" Atribute2="string" Attribute3="double number" Attribute4="string" />
            .
            .
            .
            <Parameter_3N Attribute1="double number" Attribute2="string" Attribute3="double number" Attribute4="string" />
        </Channel_Z>
    </Measurement_Data>
</DATA>

文件中的边界条件如下:

  1. 总通道数因导入的文件而异。
  2. 每个通道中的参数数量各不相同(名称和总数)。
  3. 每个通道的参数不一定相同。
  4. 每个参数都有4个完全相同的属性:2个是数字,2个是字符串。

我能够使用 XmlDocument 加载 XML 文档并使用 XElement 创建所有元素的列表,并且我能够单独为跨多个通道的单个参数生成数据表,但我不知道如何将所有内容拼凑在一起。

这是我为跨多个通道的单个参数生成数据表的代码(请注意,我还不知道如何将通道信息添加到数据表):

private void OpenButton_Click(object sender, EventArgs e)
{
    DataTable values = new DataTable();

    values.Columns.Add("Parameter");
    values.Columns.Add("Attribute1");
    values.Columns.Add("Attribute2");
    values.Columns.Add("Attribute3");
    values.Columns.Add("Attribute4");

    string filePath = @"C:/Users/.../test.xml";

    List<string> parameter = new List<string>();
    List<double> attribute1Values = new List<double>();
    List<string> attribute2Values = new List<string>();
    List<double> attribute3Values = new List<double>();
    List<string> attribute4Values = new List<string>();

    XmlDocument doc = new XmlDocument();
    doc.Load(filePath);

    XmlNodeList elemList = doc.GetElementsByTagName("Parameter_1");
    string param = "Parameter_1";

    for (int i = 0; i < elemList.Count; i++)
    {
        parameter.Add(param);
        attribute1Values.Add(Double.Parse(elemList[i].Attributes["Attribute1"].Value));
        attribute2Values.Add(elemList[i].Attributes["Attribute2"].Value);
        attribute3Values.Add(Double.Parse(elemList[i].Attributes["Attribute3"].Value));
        attribute4Values.Add(elemList[i].Attributes["Attribute4"].Value);
    }

    for (int i = 0; i < attribute1Values.Count;i++ )
    {
        var row = values.NewRow();
        row["Parameter"] = parameter[i];
        row["Attribute1"] = attribute1Values[i];
        row["Attribute2"] = attribute2Values[i];
        row["Attribute3"] = attribute3Values[i];
        row["Attribute4"] = attribute4Values[i];
        values.Rows.Add(row);
    }
    dataGridView1.DataSource = values;
}

试试这个解决方案:

private void OpenButton_Click(object sender, EventArgs e)
{
    DataTable values = new DataTable();

    values.Columns.Add("Channel");
    values.Columns.Add("Parameter");
    values.Columns.Add("Attribute1");
    values.Columns.Add("Attribute2");
    values.Columns.Add("Attribute3");
    values.Columns.Add("Attribute4");

    string filePath = @"C:/Users/.../test.xml";
    XDocument xDoc = XDocument.Load(filePath);

    var channels = from channel in xDoc.Descendants("Measurement_Data").Elements()
                   select new
                   {
                       ChannelName = channel.Name,
                       Parameters = channel.Elements().Select(a => new
                       {
                           ParameterName = a.Name,
                           Attribute1 = a.Attribute("Attribute1").Value,
                           Attribute2 = a.Attribute("Attribute2").Value,
                           Attribute3 = a.Attribute("Attribute3").Value,
                           Attribute4 = a.Attribute("Attribute4").Value
                       })

                   };

    foreach (var channel in channels)
    {
        foreach (var element in channel.Parameters)
        {
            DataRow row = values.NewRow();

            row["Channel"] = channel.ChannelName;
            row["Parameter"] = element.ParameterName;
            // If attributes are not numbers, parsing will generate error.
            row["Attribute1"] = Double.Parse(element.Attribute1);
            row["Attribute2"] = element.Attribute2;
            row["Attribute3"] = Double.Parse(element.Attribute3);
            row["Attribute4"] = element.Attribute4;

            values.Rows.Add(row);
        }
    }
    dataGridView1.DataSource = values;
}

我使用以下 XML 文档对其进行了测试:

<?xml version="1.0"?>
<DATA>
    <SystemID>
        <Information>
        </Information>
    </SystemID>
    <Measurement_Data>
        <Channel_01>
            <Parameter_1 Attribute1="123" Attribute2="string" Attribute3="456" Attribute4="string" />
            <Parameter_2 Attribute1="123" Attribute2="string" Attribute3="456" Attribute4="string" />
            <Parameter_3 Attribute1="123" Attribute2="string" Attribute3="456" Attribute4="string" />
            <Parameter_N Attribute1="123" Attribute2="string" Attribute3="456" Attribute4="string" />
        </Channel_01>
        <Channel_02>
            <Parameter_A Attribute1="123" Attribute2="string" Attribute3="456" Attribute4="string" />
            <Parameter_B Attribute1="123" Attribute2="string" Attribute3="456" Attribute4="string" />
            <Parameter_C Attribute1="123" Attribute2="string" Attribute3="456" Attribute4="string" />
            <Parameter_N Attribute1="123" Attribute2="string" Attribute3="456" Attribute4="string" />
        </Channel_02>
        <Channel_Z>
            <Parameter_2A Attribute1="123" Attribute2="string" Attribute3="456" Attribute4="string" />
            <Parameter_3A Attribute1="123" Attribute2="string" Attribute3="456" Attribute4="string" />
            <Parameter_1A Attribute1="123" Attribute2="string" Attribute3="456" Attribute4="string" />
            <Parameter_3N Attribute1="123" Attribute2="string" Attribute3="456" Attribute4="string" />
        </Channel_Z>
    </Measurement_Data>
</DATA>