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

时间:2015-08-30 19:07:22

标签: c# xml datatable

我需要将以下XML文件导入C#中的DataTable:

<?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个是字符串。
  5. 我能够使用XmlDocument加载XML文档并使用XElement创建所有元素的列表,并且我可以在多个通道中为单个参数生成DataTable,但我不知道如何将所有内容拼凑在一起。

    以下是我为多个频道的单个参数生成DataTable的代码(请注意,我不知道如何将频道信息添加到DataTable):

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

1 个答案:

答案 0 :(得分:1)

试试这个解决方案:

-webkit-filter: grayscale; /*sepia, hue-rotate, invert....*/
-webkit-filter: brightness(50%); 

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

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