一、概念及特征:
1. XML 指可擴展標記語言(Extensible Markup Language),用戶可以自己定義標簽。XML 被設計用來傳輸和存儲數據,而 HTML 用于格式化并顯示數據,并且HTML不能自定義標簽。
2. XML 文檔形成一種樹結構, XML 文檔必須包含根元素。該元素是所有其他元素的父元素。XML 文檔中的元素形成了一棵文檔樹。這棵樹從根部開始,并擴展到樹的最底端。
3. XML中所有元素都必須有關閉標簽, XML 必須正確地嵌套, XML 的屬性值須加引號, XML 標簽對大小寫敏感。
二、基本格式示例:
<bookstore><book category="CHILDREN"><title>Harry Potter</title><author>J K. Rowling</author><year>2005</year><price>29.99</price></book></bookstore>
三、使用XMLDocument讀寫XML
protected void btnCreate_Click(object sender, EventArgs e){XmlDocument xmlDoc = new XmlDocument();xmlDoc.Load(Server.MapPath("bookstore.xml"));XmlNode root = xmlDoc.SelectSingleNode("bookstore");XmlElement xe1 = xmlDoc.CreateElement("book");xe1.SetAttribute("genre", "李贊紅");xe1.SetAttribute("ISBN", "2-3631-4");XmlElement xesub1 = xmlDoc.CreateElement("title");xesub1.InnerText = "CS從入門到精通";xe1.AppendChild(xesub1);XmlElement xesub2 = xmlDoc.CreateElement("author");xesub2.InnerText = "候捷";xe1.AppendChild(xesub2);XmlElement xesub3 = xmlDoc.CreateElement("price");xesub3.InnerText = "58.3";xe1.AppendChild(xesub3);root.AppendChild(xe1);xmlDoc.Save(Server.MapPath("bookstore.xml"));} protected void EditNodes_Click(object sender, EventArgs e){XmlDocument xmlDoc = new XmlDocument();xmlDoc.Load(Server.MapPath("bookstore.xml"));XmlNodeList nodeList = xmlDoc.SelectSingleNode("bookstore").ChildNodes;foreach (XmlNode xn in nodeList){XmlElement xe = (XmlElement)xn;if (xe.GetAttribute("genre") == "李贊紅"){xe.SetAttribute("genre", "update李贊紅");XmlNodeList nls = xe.ChildNodes;foreach (XmlNode xn1 in nls){XmlElement xe2 = (XmlElement)xn1;if (xe2.Name == "author"){xe2.InnerText = "亞勝";break;}}break;}}xmlDoc.Save(Server.MapPath("bookstore.xml"));}protected void btnDelete_Click(object sender, EventArgs e){XmlDocument xmlDoc = new XmlDocument();xmlDoc.Load(Server.MapPath("bookstore.xml"));XmlNodeList xnl = xmlDoc.SelectSingleNode("bookstore").ChildNodes;foreach (XmlNode xn in xnl){XmlElement xe = (XmlElement)xn;if (xe.GetAttribute("genre") == "fantasy"){xe.RemoveAttribute("genre");}else if (xe.GetAttribute("genre") == "update李贊紅"){xe.RemoveAll();}}xmlDoc.Save(Server.MapPath("bookstore.xml"));}
四、使用LINQ to XML讀寫XML,LINQ to XML 最重要的優勢是它與 Language-Integrated Query (LINQ) 的集成。
private void CreateXmlFile(){ ///設置新的XML文件保存的地址string xmlFilePath = Server.MapPath("Books.xml");XDocument doc = new XDocument(new XDeclaration("1.0", "utf-8", "no"),new XElement("Books",new XElement("Book",new XAttribute("ID", "104"), ///添加屬性IDnew XElement("No", "0004"), ///添加元素Nonew XElement("Name", "Book 0004"), ///添加元素Namenew XElement("Price", "300"), ///添加元素Pricenew XElement("Remark", "This is a book 0004.") ///添加元素Remark )));///保存為XML文件 doc.Save(xmlFilePath);///顯示XML文件的內容 Response.Write(doc);///設置網頁顯示的形式為XML文件Response.ContentType = "text/xml";Response.End();}private void AddXmlElement(){ ///導入XML文件string xmlFilePath = Server.MapPath("Books.xml");XElement xe = XElement.Load(xmlFilePath);///創建一個新的節點XElement book = new XElement("Book",new XAttribute("ID", "105"), ///添加屬性IDnew XElement("No", "0005"), ///添加元素Nonew XElement("Name", "Book 0005"), ///添加元素Namenew XElement("Price", "500"), ///添加元素Pricenew XElement("Remark", "This is a book 0005.") ///添加元素Remark );///添加節點到文件中,并保存 xe.Add(book);xe.Save(xmlFilePath);///顯示XML文件的內容 Response.Write(xe);///設置網頁顯示的形式為XML文件Response.ContentType = "text/xml";Response.End();}private void UpdateXmlElement(){///導入XML文件string xmlFilePath = Server.MapPath("Books.xml");XElement xe = XElement.Load(xmlFilePath);///查找被替換的元素IEnumerable<XElement> element = from e in xe.Elements("Book")where e.Attribute("ID").Value == "104"select e;///替換為新元素,并保存if (element.Count() > 0){XElement first = element.First();///設置新的屬性first.SetAttributeValue("ID", "106");///替換新的節點 first.ReplaceNodes(new XElement("No", "0006"), ///添加元素Nonew XElement("Name", "Book 0006"), ///添加元素Namenew XElement("Price", "600"), ///添加元素Pricenew XElement("Remark", "This is a book 0006.") ///添加元素Remark );}xe.Save(xmlFilePath);///顯示XML文件的內容 Response.Write(xe);///設置網頁顯示的形式為XML文件Response.ContentType = "text/xml";Response.End();}private void RemoveXmlElement(){///導入XML文件string xmlFilePath = Server.MapPath("Books.xml");XElement xe = XElement.Load(xmlFilePath);///查找被刪除的元素IEnumerable<XElement> element = from e in xe.Elements()where (string)e.Element("Name") == "Book 0006"select e;///刪除指定的元素,并保存if (element.Count() > 0) { element.First().Remove(); }xe.Save(xmlFilePath);///顯示XML文件的內容 Response.Write(xe);///設置網頁顯示的形式為XML文件Response.ContentType = "text/xml";Response.End();}