利⽤c#语⾔解析xml⽂件,C#操作xml⽂件:使⽤XmlDocument
实现读取和写⼊
XML⽂件是⼀种常⽤的⽂件格式,例如WinForm⾥⾯的fig以及Web程序中的fig⽂件,还有许多重要的场所都有它的⾝影。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("l");
List lists = new List();
XmlNodeList list = doc.SelectNodes("/Table/row");
foreach (XmlNode item in list)
{
CustomerInfo cust = new CustomerInfo();
cust.Version = item.Attributes["Version"].Value;
使用dom4j解析xml文件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("l");
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("l");
3.在读取的同时进⾏修改,删除,添加
添加:
XmlDocument doc = new XmlDocument();
doc.Load("l");
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("l");
修改:
XmlDocument doc = new XmlDocument();
doc.Load("l");
XmlNode ele = doc.SelectSingleNode("descendant::row[CustomerID='ALFKI1']"); ele["CompanyName"].InnerText = "程沐喆";
doc.Save("l");
删除:
XmlDocument doc = new XmlDocument();
doc.Load("l");
XmlNode ele = doc.SelectSingleNode("descendant::row[CustomerID='ALFKI1']"); doc.DocumentElement.RemoveChild(ele);
doc.Save("l");
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论