C#操作xml文件:使用XmlDocument 实现读取和写入
xml文件是一种常用的文件格式,例如winform里面的app.config以及web程序中的web.config文件,还有许多重要的场所都有它的身影。xml是internet环境中跨平台的,依赖于内容的技术,是当前处理结构化文档信息的有力工具。xml是一种简单的数据存储语言,使用一系列简单的标记描述数据,而这些标记可以用方便的方式建立,虽然xml占用的空间比二进制数据要占用更多的空间,但xml极其简单易于掌握和使用。微软也提供了一系列类库来倒帮助我们在应用程序中存储xml文件。
“在程序中访问进而操作xml文件一般有两种模型,分别是使用dom(文档对象模型)和流模型,使用dom的好处在于它允许编辑和更新xml文档,可以随机访问文档中的数据,可以使用xpath查询,但是,dom的缺点在于它需要一次性的加载整个文档到内存中,对于大型的文档,这会造成资源问题。流模型很好的解决了这个问题,因为它对xml文件的访问采用的是流的概念,也就是说,任何时候在内存中只有当前节点,但它也有它的不足,它是只读的,仅向前的,不能在文档中执行向后导航操作。”具体参见在visual c#中使用xml指南之读取xml
下面我将介绍三种常用的读取xml文件的方法。分别是
1: 使用 xmldocument
2: 使用 xmltextreader
3: 使用 linq to xml
下面我们使用xmldocument:
1.读取元素和属性:
xmldocument doc = new xmldocument();
doc.load("customer2.xml");
list<customerinfo> lists = new list<customerinfo>();
xmlnodelist list = doc.selectnodes("/table/row");
foreach (xmlnode item in list)
{
customerinfo cust = new customerinfo();
cust.version = item.attributes["version"].value;
cust.appid = item.attributes["appid"].value;
cust.customerid = item["customerid"].innertext;
cust.companyname = item["companyname"].innertext;
cust.contactname = item["contactname"].innertext;
cust.contacttitle = item["contacttitle"].innertext;
cust.address = item["address"].innertext;
cust.city = item["city"].innertext;
cust.postalcode = item["postalcode"].innertext;
cust.country = item["country"].innertext;
cust.phone = item["phone"].innertext;
cust.fax = item["fax"].innertext;
lists.add(cust);
}
2.创建文档-属性和元素
xmldocument doc = new xmldocument();
// doc.load("customertest1.xml");
xmldeclaration xmldecl = doc.createxmldeclaration("1.0", "utf-8", null);
xmlelement root = doc.documentelement;
doc.insertbefore(xmldecl, root);
xmlelement ele = doc.createelement("table");
doc.appendchild(ele);
for (int i = 1; i < 10; i++)
{
xmlelement row = doc.createelement("row");
row.setattribute("version", "2.0");
row.setattribute("appid", "111");
xmlelement custmonerid = doc.createelement("customerid");
custmonerid.innertext = "程沐喆" + i.tostring();
row.appendchild(custmonerid);
xmlelement custmonername = doc.createelement("companyname");
custmonername.innertext = "alfreds futterkiste" + i.tostring();
row.appendchild(custmonername);
xmlelement contactname = doc.createelement("contactname");
contactname.innertext = "maria anders" + i.tostring();
row.appendchild(contactname);
xmlelement contacttitle = doc.createelement("contacttitle");
contacttitle.innertext = "sales representative" + i.tostring();
row.appendchild(contacttitle);
xmlelement address = doc.createelement("address");
address.innertext = "obere str. 57" + i.tostring();
row.appendchild(address);
xmlelement city = doc.createelement("city");
city.innertext = "berlin";
row.appendchild(city);
xmlelement postalcode = doc.createelement("postalcode");
custmonerid.innertext = "12209";
row.appendchild(postalcode);
xmlelement country = doc.createelement("country");
country.innertext = "germany";
row.appendchild(country);
xmlelement phone = doc.createelement("phonw");
phone.innertext = "030-0074321";
row.appendchild(phone);
xmlelement fax = doc.createelement("fax");
fax.innertext = "030-0076545";
row.appendchild(fax);
ele.appendchild(row);
}
doc.save("customertest2.xml");
3.在读取的同时进行修改,删除,添加
添加:
xmldocument doc = new xmldocument();
doc.load("customertest.xml");
xmlelement ele = doc.documentelement;
for (int i = 0; i < 2; i++)
{
xmlelement cust = doc.createelement("customers");
cust.setattribute("customerid","程沐喆"+i.tostring());
cust.setattribute("companyname","程沐喆"+i.tostring());
cust.setattribute("contactname", "程沐喆" + i.tostring());
cust.setattribute("contacttitle", "程沐喆" + i.tostring());
cust.setattribute("address", "obere str .57"+i.tostring());
cust.setattribute("city", "berlin");
cust.setattribute("postalcode", "12209");
cust.setattribute("country", "germany");
cust.setattribute("phone", "030-0074321");
cust.setattribute("fax", "030-0076545");
ele.appendchild(cust);
}
doc.save("customertest.xml");
修改:
xmldocument doc = new xmldocument();
doc.load("customertest1.xml");
xmlnode ele = doc.selectsinglenode("descendant::row[customerid='alfki1']");
ele["companyname"].innertext = "程沐喆";
doc.save("customertest1.xml");
删除:
xmldocument doc = new xmldocument();
doc.load("customertest1.xml");
xmlnode ele = doc.selectsinglenode("descendant::row[customerid='alfki1']");
doc.documentelement.removechild(ele);
doc.save("customertest1.xml");