gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 13:01:00 25 4
gpt4 key购买 nike

我有以下 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. 总 channel 数因导入的文件而异。
  2. 每个 channel 中的参数数量各不相同(名称和总数)。
  3. 每个 channel 的参数不一定相同。
  4. 每个参数都有4个完全相同的属性:2个是数字,2个是字符串。

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

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

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>

关于c# - 如何将具有不同数量的节点和子节点的 XML 文件导入 C# 中的 DataTable?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32300143/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com