本文介绍了从XML写入数据表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 以下是我的XML结构。 < school > < 标识 rollno = 01 > < 信息 名称 = ABC 年龄 = 21 性别 = M / > < / Identification > < / school > 我想将这种XML格式读入DataTable,以便DataTable拥有数据以下面提到的形式 Rollno姓名年龄性别 01 ABC 21 M 我怎样才能实现这个目标? 谢谢。 我是什么尝试过: XmlTextReader xmlContent = new XmlTextReader(@XMLPath); DataSet tableSet = new DataSet() ; tableSet.ReadXml(xmlContent); DataTableObject = tableSet.Tables [0]; 使用上面的代码,我能够读取XML,但是DataTable的输出并不像我在questio中指定的那样,它也在UI中创建了一个cloumn名称IdentificationID,我不知道需要解决方案 引用:它还创建了一个cloumn名称IdentificationID in我不需要的用户界面但如果仔细查看该栏中的值,则 0 。 如果你还检查了 tableSet.Tables [ 1 ] 的内容,它也有这个不需要的列 Identification_Id 并且它的值也为0. 如果将XML更改为更加明显,那么额外的列正在做什么 < school > ; < 标识 rollno = 01 > < 信息 名称 = ABC 年龄 = 21 性别 = M / > < / Identification > < 标识 rollno = 02 > < 信息 名称 = BCD 年龄 = 22 性别 = F / > < / Identification > < / school > 现在查看数据集中的两个数据表... Identification_Id = 0匹配原始数据,Identification_Id = 1将行与我的新数据项匹配。 所以你可以像这样加入两个数据表 var dt1 = tableSet.Tables [ 0 ]; // 包含标识内容 var dt2 = tableSet.Tables [ 1 ]; // 包含信息内容 var res =( from t1 in dt1.AsEnumerable() join t2 in dt2.AsEnumerable()on( int )t1 [ Identification_Id]等于( int )t2 [ Identification_Id] 选择 new { rollno = t1 [ rollno], Name = t2 [ 名称],年龄= t2 [ 年龄],性别= t2 [ 性别] })。ToList(); 或者如果您更喜欢这种格式... var res = dt1.AsEnumerable() .Join(dt2.AsEnumerable(),t1 = > ( int )t1 [ Identification_Id],t2 = > ( int )t2 [ Identification_Id],(t1,t2)= > new { rollno = t1 [ rollno], Name = t2 [ 名称],年龄= t2 [ 年龄], 性别= t2 [ 性别] })。ToList() ; 您在标签中提到了DataGrid,因此您可以使用 dataGrid1.DataSource = res; 或者如果你确实想要一个DataTable,你可以做一些像 var resultTable = new DataTable(); resultTable.Columns.Add( rollno); resultTable.Columns.Add( 名称); resultTable.Columns.Add( 年龄); resultTable.Columns.Add( Sex); foreach ( var i in res) resultTable.Rows.Add(i.rollno,i.Name,i.Age,i.Sex); 如果需要,记得处理任何空值to。 你也可以使用 XElement类(System.Xml.Linq) [ ^ ] string s = @ < ; school> < Identification rollno =01> < Information Name =ABCAge =21Sex =M/> < / Identification> < Identification rollno =02> < Information Name =BCDAge = 22性别=F/> < / Identification> < / school>; XElement xe = XElement.Parse(s); // 或者如果你有一个文件 // XElement xe = XElement.Load(fileName); var result =( from el xe.Elements( 识别) 让 subEl = el.Element( 信息) 选择 new { RollNo = int .Parse(el.Attribute( rollno)。值),名称= subEl.Attribute( 名称)。值,年龄= int .Parse(subEl.Attribute( 年龄) .Value),性别= char .Parse(subEl.Attribute( 性别)。值),})。ToList(); dataGridView.DataSource = result; 要将结果转换为DataTable,您必须按照解决方案1中的建议循环遍历列表。 Below is my XML structure.<school> <Identification rollno="01"> <Information Name="ABC" Age="21" Sex="M" /> </Identification></school>I want to read this XML format into DataTable so that DataTable has data in form of as mentioned belowRollno Name Age Sex01 ABC 21 MHow can I achieve this?Thank You.What I have tried:XmlTextReader xmlContent = new XmlTextReader(@"XMLPath");DataSet tableSet = new DataSet();tableSet.ReadXml(xmlContent);DataTableObject = tableSet.Tables[0]; With the above code I was able to read the XML but the output which DataTable had was not as I have specified in the questio and it is also creating a cloumn names IdentificationID in the UI which I don't need 解决方案 Quote:it is also creating a cloumn names IdentificationID in the UI which I don't needBut if you look carefully at the value in that column it is 0.If you also examine the contents of tableSet.Tables[1], it also has this "un-needed" column Identification_Id and it also has a value of 0.It becomes more apparent what that extra column is doing if you change your XML to be <school> <Identification rollno="01"> <Information Name="ABC" Age="21" Sex="M" /> </Identification> <Identification rollno="02"> <Information Name="BCD" Age="22" Sex="F" /> </Identification> </school>Now look at the two datatables in the dataset ... Identification_Id = 0 matches up your original data and Identification_Id = 1 matches up the rows with my new data item.So you can JOIN the two data tables together like thisvar dt1 = tableSet.Tables[0]; // contains the identification stuffvar dt2 = tableSet.Tables[1]; // contains the Information stuffvar res = (from t1 in dt1.AsEnumerable() join t2 in dt2.AsEnumerable() on (int) t1["Identification_Id"] equals (int) t2["Identification_Id"] select new { rollno = t1["rollno"], Name = t2["Name"], Age = t2["Age"], Sex = t2["Sex"] }).ToList();or if you prefer this format... var res = dt1.AsEnumerable() .Join(dt2.AsEnumerable(), t1 => (int) t1["Identification_Id"], t2 => (int) t2["Identification_Id"], (t1, t2) => new { rollno = t1["rollno"], Name = t2["Name"], Age = t2["Age"], Sex = t2["Sex"] }).ToList();You've mentioned DataGrid in your tags so you could just use dataGrid1.DataSource = res;Or if you really do want a DataTable as a result you can do something like var resultTable = new DataTable();resultTable.Columns.Add("rollno");resultTable.Columns.Add("Name");resultTable.Columns.Add("Age");resultTable.Columns.Add("Sex");foreach (var i in res) resultTable.Rows.Add(i.rollno, i.Name, i.Age, i.Sex);remembering to handle any null values if you need to.You can also do basically the same thing using the XElement Class (System.Xml.Linq)[^]string s = @"<school> <Identification rollno=""01""> <Information Name=""ABC"" Age=""21"" Sex=""M"" /> </Identification> <Identification rollno=""02""> <Information Name=""BCD"" Age=""22"" Sex=""F"" /> </Identification></school>";XElement xe = XElement.Parse(s);// Or if you have a file// XElement xe = XElement.Load(fileName);var result = (from el in xe.Elements("Identification") let subEl = el.Element("Information") select new { RollNo = int.Parse(el.Attribute("rollno").Value), Name = subEl.Attribute("Name").Value, Age = int.Parse(subEl.Attribute("Age").Value), Sex = char.Parse(subEl.Attribute("Sex").Value), }).ToList();dataGridView.DataSource = result;To convert the result to a DataTable, you have to loop through the list as suggested in Solution 1. 这篇关于从XML写入数据表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-22 13:42