C# 操作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");

 

posted @ 2019-11-06 10:34  小飛  阅读(214)  评论(0编辑  收藏  举报