C#操作XML总结
1、using System.Xml;
using System.Xml; //初始化一個xml實例 XmlDocument xml=new XmlDocument();//導入指定xml文件 xml.Load(path); xml.Load(HttpContext.Current.Server.MapPath("~/file/bookstore.xml"));//指定一個節(jié)點 XmlNode root=xml.SelectSingleNode("/root");//獲取節(jié)點下所有直接子節(jié)點 XmlNodeList childlist=root.ChildNodes;//判斷該節(jié)點下是否有子節(jié)點 root.HasChildNodes;//獲取同名同級節(jié)點集合 XmlNodeList nodelist=xml.SelectNodes("/Root/News");//生成一個新節(jié)點 XmlElement node=xml.createElement_x("News");//將節(jié)點加到指定節(jié)點下,作為其子節(jié)點 root.AppendChild(node);//將節(jié)點加到指定節(jié)點下某個子節(jié)點前 root.InsertBefore(node,root.ChildeNodes[i]);//為指定節(jié)點的新建屬性并賦值 node.SetAttribute("id","11111");//為指定節(jié)點添加子節(jié)點 root.AppendChild(node);//獲取指定節(jié)點的指定屬性值 string id=node.Attributes["id"].Value;//獲取指定節(jié)點中的文本 string content=node.InnerText;//保存XML文件 string path=Server.MapPath("~/file/bookstore.xml"); xml.Save(path); //or use :xml.Save(HttpContext.Current.Server.MapPath("~/file/bookstore.xml"));2、XmlTextWriter xmlWriter;
XmlTextWriter xmlWriter; //導入指定xml文件string strFilename = Server.MapPath("data1.xml") ; //創(chuàng)建一個xml文檔 xmlWriter = new XmlTextWriter(strFilename,Encoding.Default);xmlWriter.Formatting = Formatting.Indented;xmlWriter.WriteStartDocument();xmlWriter.WriteStartElement("Employees");xmlWriter.WriteStartElement("Node");xmlWriter.WriteAttributeString("genre","李贊紅");xmlWriter.WriteAttributeString("ISBN","2-3631-4");xmlWriter.WriteStartElement("title");xmlWriter.WriteString("CS從入門到精通");xmlWriter.WriteEndElement();xmlWriter.WriteStartElement("author");xmlWriter.WriteString("候捷");xmlWriter.WriteEndElement();xmlWriter.WriteStartElement("price");xmlWriter.WriteString("58.3");xmlWriter.WriteEndElement();xmlWriter.WriteEndElement();xmlWriter.Close(); //2,添加一個結點:XmlDocument xmlDoc=new XmlDocument(); xmlDoc.Load(Server.MapPath("data.xml")); XmlNode root=xmlDoc.SelectSingleNode("Employees");//查找<Employees> XmlElement xe1=xmlDoc.CreateElement("Node");//創(chuàng)建一個<Node>節(jié)點 xe1.SetAttribute("genre","張三");//設置該節(jié)點genre屬性 xe1.SetAttribute("ISBN","1-1111-1");//設置該節(jié)點ISBN屬性 XmlElement xesub1=xmlDoc.CreateElement("title"); xesub1.InnerText="C#入門幫助";//設置文本節(jié)點 xe1.AppendChild(xesub1);//添加到<Node>節(jié)點中 XmlElement xesub2=xmlDoc.CreateElement("author"); xesub2.InnerText="高手"; xe1.AppendChild(xesub2); XmlElement xesub3=xmlDoc.CreateElement("price"); xesub3.InnerText="158.3"; xe1.AppendChild(xesub3);root.AppendChild(xe1);//添加到<Employees>節(jié)點中 xmlDoc.Save ( Server.MapPath("data.xml") );//3,修改結點的值(屬性和子結點):XmlDocument xmlDoc=new XmlDocument(); xmlDoc.Load( Server.MapPath("data.xml") );XmlNodeList nodeList=xmlDoc.SelectSingleNode("Employees").ChildNodes;//獲取Employees節(jié)點的所有子節(jié)點foreach(XmlNode xn in nodeList)//遍歷所有子節(jié)點 { XmlElement xe=(XmlElement)xn;//將子節(jié)點類型轉換為XmlElement類型 if(xe.GetAttribute("genre")=="張三")//如果genre屬性值為“張三” { xe.SetAttribute("genre","update張三");//則修改該屬性為“update張三” XmlNodeList nls=xe.ChildNodes;//繼續(xù)獲取xe子節(jié)點的所有子節(jié)點 foreach(XmlNode xn1 in nls)//遍歷 { XmlElement xe2=(XmlElement)xn1;//轉換類型 if(xe2.Name=="author")//如果找到 { xe2.InnerText="亞勝";//則修改 } } } } xmlDoc.Save( Server.MapPath("data.xml") );//保存。//4,修改結點(添加結點的屬性和添加結點的自結點): XmlDocument xmlDoc=new XmlDocument(); xmlDoc.Load( Server.MapPath("data.xml") );XmlNodeList nodeList=xmlDoc.SelectSingleNode("Employees").ChildNodes;//獲取Employees節(jié)點的所有子節(jié)點foreach(XmlNode xn in nodeList) { XmlElement xe=(XmlElement)xn; xe.SetAttribute("test","111111");XmlElement xesub=xmlDoc.CreateElement("flag"); xesub.InnerText="1"; xe.AppendChild(xesub); } xmlDoc.Save( Server.MapPath("data.xml") );//5,刪除結點中的某一個屬性: XmlDocument xmlDoc=new XmlDocument(); xmlDoc.Load( Server.MapPath("data.xml") ); XmlNodeList xnl=xmlDoc.SelectSingleNode("Employees").ChildNodes; foreach(XmlNode xn in xnl) { XmlElement xe=(XmlElement)xn; xe.RemoveAttribute("genre");//刪除genre屬性 XmlNodeList nls=xe.ChildNodes;//繼續(xù)獲取xe子節(jié)點的所有子節(jié)點 foreach(XmlNode xn1 in nls)//遍歷 { XmlElement xe2=(XmlElement)xn1;//轉換類型 if(xe2.Name=="flag")//如果找到 { xe.RemoveChild(xe2);//則刪除 } } } xmlDoc.Save( Server.MapPath("data.xml") );//] 6,刪除結點: XmlDocument xmlDoc=new XmlDocument(); xmlDoc.Load( Server.MapPath("data.xml") ); XmlNode root=xmlDoc.SelectSingleNode("Employees"); XmlNodeList xnl=xmlDoc.SelectSingleNode("Employees").ChildNodes; for(int i=0;i<xnl.Count;i++) { XmlElement xe=(XmlElement)xnl.Item(i); if(xe.GetAttribute("genre")=="張三") { root.RemoveChild(xe); if(i<xnl.Count)i=i-1; } } xmlDoc.Save( Server.MapPath("data.xml") );//]7,按照文本文件讀取xmlSystem.IO.StreamReader myFile =new System.IO.StreamReader(Server.MapPath("data.xml"),System.Text.Encoding.Default); //注意System.Text.Encoding.Defaultstring myString = myFile.ReadToEnd();//myString是讀出的字符串 myFile.Close();3、xml to linq
XDocument doc = new XDocument( ///創(chuàng)建XDocument類的實例 new XDeclaration("1.0", "utf-8", "yes"),///XML的聲明,包括版本,編碼,xml文件是否獨立 new XElement("Books", ///添加根節(jié)點 new XElement("Book", ///添加一個節(jié)點 new XAttribute("BookID", "001"),///添加屬性BookID new XElement("BookNo", "0001"), ///添加元素BookNo new XElement("BookName", "Book 0001"),///添加元素BookName new XElement("BookPrice", "40"),///添加元素BookPrice new XElement("BookRemark", "This is a book 0001")///添加元素BookRemark ) ) ); ///保存XML文件到指定地址 doc.Save(@"C:\Books.xml");2.添加元素
///導入XML文件 XElement xe = XElement.Load(@"C:\Books.xml"); ///創(chuàng)建一個新節(jié)點 XElement book1 = new XElement("Book", new XAttribute("BookID", "002"), new XElement("BookNo", "0002"), new XElement("BookName", "Book 0002"), new XElement("BookPrice", "50"), new XElement("BookRemark", "This is a book 0002") ); ///添加節(jié)點到XML文件中,并保存 xe.Add(book1); ///創(chuàng)建一個新節(jié)點 XElement book2 = new XElement("Book", new XAttribute("BookID", "003"), new XElement("BookNo", "0003"), new XElement("BookName", "Book 0003"), new XElement("BookPrice", "30"), new XElement("BookRemark", "This is a book 0003") ); ///添加節(jié)點到XML文件中,并保存 xe.Add(book2); ///創(chuàng)建一個新節(jié)點 XElement book3 = new XElement("Book", new XAttribute("BookID", "004"), new XElement("BookNo", "0004"), new XElement("BookName", "Book 0004"), new XElement("BookPrice", "60"), new XElement("BookRemark", "This is a book 0004") ); ///添加節(jié)點到XML文件中 xe.Add(book3); ///保存到XML文件中 xe.Save(@"C:\Books.xml");3.修改元素
XElement xe = XElement.Load(@"C:\Books.xml"); ///查詢修改的元素 IEnumerable<XElement> element = from e in xe.Elements("Book") where e.Attribute("BookID").Value == "xxx" //xxx指定的修改元素 select e; ///修改元素 if (element.Count() > 0) { XElement firstelement = element.First(); ///設置新的屬性 firstelement.SetAttributeValue("BookID", "new004"); ///替換成新的節(jié)點 firstelement.ReplaceNodes( new XElement("BookNo", "new0004"), new XElement("BookName", "Book new0004"), new XElement("BookPrice", "45"), new XElement("BookRemark", "This is a book new0004") ); } xe.Save(@"C:\Books.xml");4.刪除元素
XElement xe = XElement.Load(@"C:\Books.xml"); ///查詢修改的元素 IEnumerable<XElement> element = from e in xe.Elements("Book") where e.Attribute("BookID").Value == "xxx" //xxx 指定刪除元素 select e; ///修改元素 if (element.Count() > 0) { XElement firstelement = element.First(); ///刪除此元素的所有節(jié)點和屬性 firstelement.RemoveAll(); ///刪除此元素的屬性 //firstelement.RemoveAttributes(); ///刪除此元素的子節(jié)點 //firstelement.RemoveNodes(); } xe.Save(@"C:\Books.xml");5.一些常用查詢
XElement xe = XElement.Load(@"C:\Books.xml");//查詢元素并排序 var elements = xe.Elements("Book").Where(e => Convert.ToInt32(e.Attribute("BookID").Value.Substring(e.Attribute("BookID").Value.Length - 1, 1)) > 1).OrderByDescending(e => (string)e.Element("BookName")).ToList();//查詢指定元素的子元素var elements = xe.Elements("Book").Descendants("xxx") //xxx 指定元素.ToList();//查詢指定屬性的元素var eAttribute = xe.Elements("Book") .Where(e => (string)e.Attribute("BookID") == "xxx") //xxx 指定屬性.OrderBy(e => e.Element("BookID")).ToList();//查詢指定名稱的元素var elements = xe.Elements("Book").Where(e => (string)e.Element("BookName") == "xxx") //指定元素名稱.OrderBy(e => e.Element("BookID")).ToList();轉載于:https://www.cnblogs.com/tranw/p/6050690.html
總結
- 上一篇: NOIP模拟题——dun
- 下一篇: Sqoop_ 简单介绍