C# 读取XML文件示例
有关XML文件编写规范,请参考:http://www.w3school.com.cn/xml/index.asp
XML内容如下(文件名为:Information.xml):
浏览器显示:
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
namespace WindowsFormsApplication7
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
ListViewItem listViewItem;
ListViewItem.ListViewSubItem listViewSubItem;
XmlDocument xml;
private void Form1_Load(object sender, EventArgs e)
{
listView1.Columns.Add("ID", 70, HorizontalAlignment.Center);
listView1.Columns.Add("Name", 70, HorizontalAlignment.Center);
listView1.Columns.Add("Age", 70, HorizontalAlignment.Center);
listView1.Columns.Add("Gender", 70, HorizontalAlignment.Center);
listView1.Columns.Add("City", 70, HorizontalAlignment.Center);
xml = new XmlDocument();
xml.Load("Information.xml");
XmlNodeReader reader = new XmlNodeReader(xml);
string str="";
reader.Read();
reader.Read();
while (reader.Read())
{
switch (reader.NodeType)
{
case XmlNodeType.Element:
str = reader.Name;
break;
case XmlNodeType.Text:
if (str == "ID")
{
listViewItem=listView1.Items.Add(reader.Value);
break;
}
else
{
listViewSubItem= listViewItem.SubItems.Add(reader.Value);
}
break;
}
}
}
}
}
![](http://images0.cnblogs.com/blog/580518/201311/09152243-456dfddb10aa40e48334ed61f9802b38.png)
作者:CNXY Github:https://www.github.com/cnxy 出处:http://cnxy.me 本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。 如果文中有什么错误,欢迎指出,谢谢! |