如何在c#中导入具有可变数目节点和子节点的XML文件到DataTable中
本文关键字:子节点 节点 XML 文件 DataTable 导入 | 更新日期: 2023-09-27 18:10:23
我有以下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>
以下是文件中的边界条件:
- 根据导入的文件不同,总通道数不同。
- 每个通道的参数数量不同(包括名称和总数)。
- 每个通道的参数不一定相同。
- 每个参数有完全相同的4个属性:2个是数字,2个是字符串。
我能够使用XmlDocument加载XML文档,并使用XElement创建所有元素的列表,另外,我能够跨多个通道为单个参数生成DataTable,但我不知道如何将所有内容拼凑在一起。
下面是我的代码,用于跨多个通道为单个参数生成DataTable(注意,我还不知道如何将Channel信息添加到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;
}
试试这个解决方案:
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>