可读取多行内容,仅供参考
1.xml文件内容:
文件
√
C:\Users\mirror\Desktop\备份资料.txt
未备份
文件
√
C:\Users\mirror\Desktop\待办.txt
未备份
文件夹
√
E:\Videos
未备份
文件夹
√
E:\Pictures
未备份
2.新建的类
public class FilesAuto
{
public FilesAuto()
{ }
private string fileType;
public string FileType
{
get { return fileType; }
set { fileType = value; }
}
private string fileState;
public string FileState
{
get { return fileState; }
set { fileState = value; }
}
private string filePath;
public string FilePath
{
get { return filePath; }
set { filePath = value; }
}
private string fileText;
public string FileText
{
get { return fileText; }
set { fileText = value; }
}
}
3.读取xml数据到ListView控件中:
private void ShowXML() //显示xml数据
{
string path = "filesAuto.xml"; //xml的文件位置
XmlDocument xmlDoc = new XmlDocument();
XmlReaderSettings settings = new XmlReaderSettings();
settings.IgnoreComments = true; //忽略xml文档中的注释
XmlReader reader = XmlReader.Create(path, settings);
xmlDoc.Load(reader); //读取xml数据
XmlNode xn = xmlDoc.SelectSingleNode("filesauto");
XmlNodeList xnl = xn.ChildNodes;
ListstsList = new List ();
//循环遍历获取xml文档中的全部数据
foreach (XmlNode Xnl in xnl)
{
FilesAuto filesAuto = new FilesAuto();
XmlElement xe1 = (XmlElement)Xnl;
XmlNodeList xnl0 = xe1.ChildNodes;
filesAuto.FileType = xnl0.Item(0).InnerText;
filesAuto.FileState = xnl0.Item(1).InnerText;
filesAuto.FilePath = xnl0.Item(2).InnerText;
filesAuto.FileText = xnl0.Item(3).InnerText;
ListViewItem list = listView1.Items.Add(filesAuto.FileType);
list.SubItems.Add(filesAuto.FileState);
list.SubItems.Add(filesAuto.FilePath);
list.SubItems.Add(filesAuto.FileText);
}
reader.Close(); //关闭读取流
}
使用方法:
private void Button6_Click(object sender, EventArgs e)
{
ShowXML();
}
效果图:
若有不足之处恳请大家指出改正
功能实现如下: 你上面的xml文档将它命名为"txml",拷到bin目录下的debug里面:
1:先建一个XMLr类,用于解悉xml,并定义变量存储解悉到的Name1,Sex1,Number1,Class1,ID1的值
代码如下:
public class XMLr
{
string name;
public string Name
{
get { return name; }
set { name = value; }
}
string sex;
public string Sex
{
get { return sex; }
set { sex = value; }
}
string Number;
public string Number1
{
get { return Number; }
set { Number = value; }
}
string clas;
public string Clas
{
get { return clas; }
set { clas = value; }
}
string id;
public string Id
{
get { return id; }
set { id = value; }
}
//
//
//
//
public void ReadXML()
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("txml.xml");
XmlNode docNode = xmlDoc.DocumentElement;
XmlNode objNode = docNode.FirstChild;
foreach (XmlNode allNode in objNode.ChildNodes )
{
if (allNode.Name == "Name1")
{
name = allNode.InnerText;
}
if (allNode.Name == "Sex1")
{
sex = allNode.InnerText;
}
if (allNode.Name == "Number1")
{
Number = allNode.InnerText;
}
if (allNode.Name == "Class1")
{
clas = allNode.InnerText;
}
if (allNode.Name == "ID1")
{
id = allNode.InnerText;
}
}
}
}
2: 在窗口中添加一个listView控件,在窗口加载的事件中把解悉到的XML增加到listView中,代码如下:
private void Form1_Load(object sender, EventArgs e)
{
listView1.View = View.Details;
listView1.Columns.Add("姓名");
listView1.Columns.Add("性别");
listView1.Columns.Add("学号");
listView1.Columns.Add("班级");
listView1.Columns.Add("ID");
XMLr x = new XMLr();
x.ReadXML();
ListViewItem list= listView1.Items.Add(x.Name );
list.SubItems .Add (x.Sex );
list.SubItems.Add(x.Number1 );
list.SubItems.Add(x.Clas );
list.SubItems.Add(x.Id );
}