C#如何读取xml文件内容并用listview控件显示
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C#如何读取xml文件内容并用listview控件显示相关的知识,希望对你有一定的参考价值。
xml如下所示:<?xml version="1.0" encoding="GB2312" standalone="yes" ?>
<docs>
<obj>
<Name1>李华</Name1>
<Sex1>男</Sex1>
<Number1>2011013</Number1>
<Class1>35111</Class1>
<ID1>0000</ID1>
</obj>
</docs>
1.xml文件内容:
<?xml version="1.0" encoding="utf-8"?>
<filesauto>
<file>
<type>文件</type>
<state>√</state>
<path>C:\\Users\\mirror\\Desktop\\备份资料.txt</path>
<text>未备份</text>
</file>
<file>
<type>文件</type>
<state>√</state>
<path>C:\\Users\\mirror\\Desktop\\待办.txt</path>
<text>未备份</text>
</file>
<folder>
<type>文件夹</type>
<state>√</state>
<path>E:\\Videos</path>
<text>未备份</text>
</folder>
<folder>
<type>文件夹</type>
<state>√</state>
<path>E:\\Pictures</path>
<text>未备份</text>
</folder>
</filesauto>
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;
List<FilesAuto> stsList = new List<FilesAuto>();
//循环遍历获取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();
效果图:
若有不足之处恳请大家指出改正
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;
//<Sex1>男</Sex1>
//<Number1>2011013</Number1>
//<Class1>35111</Class1>
//<ID1>0000</ID1>
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 );
C#ListView控件OleDb方法读取EXCEL
我是一个初学者,用OleDb方法读取EXCEL,发现,excel中只有一条记录的时候,程序会在序号列多出一个2,而excel要有两行以上的记录,就没有问题,如下图所示:
下面是C#代码
using System;
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.Data.OleDb;
using System.IO;
private void button1_Click(object sender, EventArgs e)
OpenFileDialog dialog = new OpenFileDialog();
dialog.Filter = "Excel文件(*xls)|*xls";
dialog.FileName = "";
if (dialog .ShowDialog()==DialogResult.OK)
string PathString = dialog.FileName.Trim(); //定义文件路径
this.listView1.Items.Clear();//先清空表格
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + PathString + ";" + "Extended Properties ='Excel 8.0;HDR=NO;IMEX=1';";
string strExcel = "select*from [sheet1$]";//指定命令对象的命令文本
OleDbConnection conn = null; //定义一个OleDbConnection对象
try
conn = new OleDbConnection(strConn ); //实例化
conn.Open();
OleDbCommand cmd = new OleDbCommand(strExcel,conn ); //定义命令对象
OleDbDataReader rears = cmd.ExecuteReader(); //执行命令对象,并用reas指向结果集
int i =1;
while (rears .Read ()) // 读取一条数据
string profile = rears[0].ToString();
ListViewItem Lv = new ListViewItem();
Lv.SubItems[0].Text = i.ToString();
i++;
Lv.SubItems.Add(profile);
listView1.Items.Add(Lv);
catch (OleDbException ex)
MessageBox.Show (("打开错误!" + ex.Message));
finally
conn.Close();
Excel 的编号是冲0开始的!你从0开始的时候添加,
还有,你可以去看看NPOI,对于Excel的操作,我建议你使用NPOI来操作,非常的方便。追问
即使是从0开始,也会出现上面的问题,只不过第二行序列号处不是2了,而是1.请高手试试。
以上是关于C#如何读取xml文件内容并用listview控件显示的主要内容,如果未能解决你的问题,请参考以下文章