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");