由于子表中的名称相同,DataSet 错误地将 XML 数据输出到 dataGridView1。
这是我的代码...
DataSet dataSet = new DataSet("Language");
private void button1_Click_1(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "XML|*.xml";
if (ofd.ShowDialog() == DialogResult.OK)
{
try
{
List<string> listTableName = new List<string>();
XmlReader xmlFile = XmlReader.Create(ofd.FileName, new XmlReaderSettings());
dataSet.ReadXml(xmlFile);
for (int i = 0; i <= dataSet.Tables.Count - 1; i++)
if (dataSet.Tables[i].ChildRelations.Count != 0)
comboBox1.Items.Add(dataSet.Tables[i].TableName);
dataGridView1.DataSource = dataSet.Tables[0].DefaultView;
xmlFile.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox1.SelectedIndex == 0)
dataGridView1.DataSource = dataSet.Tables[comboBox1.SelectedIndex].DefaultView;
else
dataGridView1.DataSource = dataSet.Tables[comboBox1.Text].ChildRelations[0].ChildTable.DefaultView;
}
这是 xml
<?xml version="1.0" encoding="utf-8"?>
<Language xmlns:dt="some-xml-namespace-here" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Name>English</Name>
<Code>EN</Code>
<Font>font_english</Font>
<Reagents>
<RecordReagent>
<Key>Flour</Key>
<Value>Flour</Value>
<Unit>g</Unit>
</RecordReagent>
<RecordReagent>
<Key>Milk</Key>
<Value>Milk</Value>
<Unit>ml</Unit>
</RecordReagent>
</Reagents>
<Gases>
<Record>
<Key>Oxygen</Key>
<Value>Oxygen</Value>
</Record>
<Record>
<Key>CarbonDioxide</Key>
<Value>Carbon Dioxide</Value>
</Record>
<Record>
<Key>Nitrogen</Key>
<Value>Nitrogen</Value>
</Record>
</Gases>
<Actions>
<Record>
<Key>Reload</Key>
<Value>Reload</Value>
</Record>
<Record>
<Key>123124</Key>
<Value>1323123</Value>
</Record>
</Actions>
<ScreenSpaceToolTips />
</Language>
问题是气体和动作表具有相同的记录表,而不是它们自己的。在俄罗斯论坛上,我没有收到有关如何制作的答案,因此在选择comboBox1 Gases时,会选择其记录表而不是通用表(而代码应该是通用的,无需指定表名称)
尝试使用 Xml Linq 执行以下代码
using System;
using System.Linq;
using System.Collections.Generic;
using System.Data;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApp10
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
XDocument doc = XDocument.Load(FILENAME);
DataSet language = new DataSet("Language");
DataTable dt = new DataTable("Reagents");
language.Tables.Add(dt);
dt.Columns.Add("Key", typeof(string));
dt.Columns.Add("Value", typeof(string));
dt.Columns.Add("Unit", typeof(string));
foreach (XElement reagents in doc.Descendants("RecordReagent"))
{
string key = (string)reagents.Element("Key");
string value = (string)reagents.Element("Value");
string unit = (string)reagents.Element("Unit");
dt.Rows.Add(new { key, value, unit });
}
XElement gases = doc.Descendants("Gases").FirstOrDefault();
dt = new DataTable("Gases");
language.Tables.Add(dt);
dt.Columns.Add("Key", typeof(string));
dt.Columns.Add("Value", typeof(string));
foreach (XElement record in gases.Descendants("Record"))
{
string key = (string)record.Element("Key");
string value = (string)record.Element("Value");
dt.Rows.Add(new { key, value});
}
XElement actions = doc.Descendants("Actions").FirstOrDefault();
dt = new DataTable("Actions");
language.Tables.Add(dt);
dt.Columns.Add("Key", typeof(string));
dt.Columns.Add("Value", typeof(string));
foreach (XElement record in actions.Descendants("Record"))
{
string key = (string)record.Element("Key");
string value = (string)record.Element("Value");
dt.Rows.Add(new { key, value });
}
}
}
}