C#读取XML文件

--硬盘Xml文件存储路径:d:\xmlFile\Testxml.xml

xml文件内容:


  
    245575913
    Tab
    
      
        1
        245575913
        ID
      
      
        2
        245575913
        name
      
    
  
  
    277576027
    Tab2
    
      
        3
        277576027
        ID
      
      
        4
        277576027
        Name2
      
      
        5
        277576027
        TabID
      
    
  

-- --打开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运行结果:

C#读取XML文件_第1张图片

转载于:https://www.cnblogs.com/wuxi88/p/5466727.html

你可能感兴趣的:(C#读取XML文件)