利用c#语言解析xml文件,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 lists = new List();

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

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C#操作XML文件需要使用System.Xml命名空间下的类。 以下是一个简单的示例,演示如何将数据写入XML文件并从中读取数据: ```csharp using System; using System.Xml; class Program { static void Main(string[] args) { // 创建XML文档对象 XmlDocument xmlDoc = new XmlDocument(); // 创建XML声明 XmlDeclaration xmlDec = xmlDoc.CreateXmlDeclaration("1.0", "UTF-8", null); // 添加XML声明到XML文档 xmlDoc.AppendChild(xmlDec); // 创建根元素 XmlElement root = xmlDoc.CreateElement("students"); // 将根元素添加到XML文档 xmlDoc.AppendChild(root); // 创建子元素 XmlElement student = xmlDoc.CreateElement("student"); XmlAttribute id = xmlDoc.CreateAttribute("id"); id.Value = "001"; student.Attributes.Append(id); XmlElement name = xmlDoc.CreateElement("name"); name.InnerText = "Tom"; student.AppendChild(name); XmlElement age = xmlDoc.CreateElement("age"); age.InnerText = "18"; student.AppendChild(age); // 将子元素添加到根元素 root.AppendChild(student); // 保存XML文档 xmlDoc.Save("students.xml"); // 读取XML文档 xmlDoc.Load("students.xml"); // 获取根元素 root = xmlDoc.DocumentElement; // 遍历子元素 foreach (XmlElement ele in root.ChildNodes) { Console.WriteLine("Student ID: " + ele.Attributes["id"].Value); Console.WriteLine("Name: " + ele["name"].InnerText); Console.WriteLine("Age: " + ele["age"].InnerText); } Console.ReadLine(); } } ``` 上述代码将创建一个名为“students.xml”的XML文件,其中包含一个名为“students”的根元素和一个名为“student”的子元素。程序会将数据写入该文件,然后从文件中读取数据并将其打印出来。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值