首页

c# 读取xml数据到datagrid中实例 附完整源码

c#

2020-8-29

读取address.xml中的数据,并显示到datagrid中

address.xml:

 

<Address_Table>
  <address>
    <name>dongdong</name>
    <Email>dongdong@163.com</Email>
    <mobile>13912345678</mobile>
    <phone>67681234</phone>
    <home>XiTu City Road No.23</home>
  </address>
  <address>
    <name>xixi</name>
    <Email>xixi@21.cn</Email>
    <mobile>13812345678</mobile>
    <phone>81234567</phone>
    <home>TaiLan Mansion No.1008 Room</home>
  </address>
  <address>
    <name>chai</name>
    <Email>cf@263.net</Email>
    <mobile>13109876543</mobile>
    <phone>98768765</phone>
    <home>MaiLei Road No.8</home>
  </address>
</Address_Table>

读取代码:

 

        // 申明一个数据集
		DataSet dsAddress = new DataSet("address");
		// 读取XML文件,并将读到的数据内容显示在DataGrid组件中。
		private void btnReadXML_Click(object sender, System.EventArgs e)
		{
			string filePath = "address.xml";
			dsAddress.ReadXml(filePath);
			dataGrid1.DataSource = dsAddress;
			dataGrid1.DataMember = "address";
			dataGrid1.CaptionText = dataGrid1.DataMember;
		}
		// 显示XML文件的框件结构
		private void btnShowSchema_Click(object sender, System.EventArgs e)
		{
			System.IO.StringWriter swXML = new System.IO.StringWriter();
			dsAddress.WriteXmlSchema(swXML);
			textBox1.Text = swXML.ToString();
		}

 

资源下载此资源下载价格为2D币(VIP免费),请先
资源文件列表
╢┴╚íXML╩²╛▌╘┤╩╡└²/address.xml , 597
╢┴╚íXML╩²╛▌╘┤╩╡└²/App.ico , 1078
╢┴╚íXML╩²╛▌╘┤╩╡└²/AssemblyInfo.cs , 1846
╢┴╚íXML╩²╛▌╘┤╩╡└²/bin/Debug/address.xml , 597
╢┴╚íXML╩²╛▌╘┤╩╡└²/bin/Debug/ReadingXML.exe , 8704
╢┴╚íXML╩²╛▌╘┤╩╡└²/bin/Debug/ReadingXML.pdb , 15872
╢┴╚íXML╩²╛▌╘┤╩╡└²/Form1.cs , 5589
╢┴╚íXML╩²╛▌╘┤╩╡└²/Form1.resx , 5315
╢┴╚íXML╩²╛▌╘┤╩╡└²/obj/Debug/ReadingXML.exe , 8704
╢┴╚íXML╩²╛▌╘┤╩╡└²/obj/Debug/ReadingXML.exe.incr , 1700
╢┴╚íXML╩²╛▌╘┤╩╡└²/obj/Debug/ReadingXML.Form1.resources , 324
╢┴╚íXML╩²╛▌╘┤╩╡└²/obj/Debug/ReadingXML.pdb , 15872
╢┴╚íXML╩²╛▌╘┤╩╡└²/obj/Debug/ReadingXML.projdata , 5512
╢┴╚íXML╩²╛▌╘┤╩╡└²/ReadingXML.csproj , 4411
╢┴╚íXML╩²╛▌╘┤╩╡└²/ReadingXML.csproj.user , 1772
╢┴╚íXML╩²╛▌╘┤╩╡└²/ReadingXML.sln , 916
╢┴╚íXML╩²╛▌╘┤╩╡└²/ReadingXML.suo , 7168
没有账号? 忘记密码?

社交账号快速登录