C#读取XML文件
--硬盘Xml文件存储路径:d:\xmlFile\Testxml.xml
xml文件内容:
<Root> <Tab> <ID>245575913</ID> <name>Tab</name> <Tabs> <Tab2> <ID>1</ID> <TabID>245575913</TabID> <Name2>ID</Name2> </Tab2> <Tab2> <ID>2</ID> <TabID>245575913</TabID> <Name2>name</Name2> </Tab2> </Tabs> </Tab> <Tab> <ID>277576027</ID> <name>Tab2</name> <Tabs> <Tab2> <ID>3</ID> <TabID>277576027</TabID> <Name2>ID</Name2> </Tab2> <Tab2> <ID>4</ID> <TabID>277576027</TabID> <Name2>Name2</Name2> </Tab2> <Tab2> <ID>5</ID> <TabID>277576027</TabID> <Name2>TabID</Name2> </Tab2> </Tabs> </Tab> </Root>
----打开Visual Studio—创建项目—选择【控制台应用程序】
#region Using Directives using System; using System.Data; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; #endregion namespace TestReadingxml { class Program { static void Main(string[] args) { DataSet thisDataSet = new DataSet(); thisDataSet.ReadXml(@"d:\xmlFile\Testxml.xml"); DataRelation custRel = thisDataSet.Relations.Add("Cust",thisDataSet.Tables[0].Columns["ID"],thisDataSet.Tables[2].Columns["TabID"]); foreach (DataRow custRow in thisDataSet.Tables["Tab"].Rows) { Console.WriteLine("ID:{0} \t Name:{1}",custRow["ID"],custRow["Name"]); foreach (DataRow detailRow in custRow.GetChildRows(custRel)) { Console.WriteLine("\t ID:{0}\tTabID:{1}\t Name2:{2};\t{3}",detailRow["ID"],detailRow["TabID"],detailRow[2],detailRow.GetParentRow(custRel)["ID"]); } } Console.WriteLine("Table created by ChildTable:{0}",thisDataSet.Tables[2].TableName); Console.WriteLine("Table created by ReadXml is called {0}",thisDataSet.Tables[0].TableName); Console.Write("Program finished,press Enter/Return to continue:"); Console.ReadKey(); } } }
--按F5运行结果: