using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Xml; using System.Web; using System.Xml.Linq; namespace XMLOperation { class Program { static void Main(string[] args) { /*=============Linq 讀寫XML==================*/ string wxmlPath = @"F:\XmlTest\test.xml"; XmlWriteReadLinqOperation writeReadLinq = new XmlWriteReadLinqOperation(); // writeReadLinq.WriteXml(wxmlPath); writeReadLinq.CreatXmlTree(wxmlPath); //string xmlPath = @"F:\XML.xml"; string xmlPath = @"C:\Users\zery.zhang\Desktop\ProjectDemo\XML.xml"; /* * 1 三者之間的關係用圖畫出 * 2 XMLElement 主要是針對節點的一些屬性進行操做 * 3 XMLDocument 主要是針對節點的CUID操做 * 4 XMLNode 爲抽象類,作爲以上兩類的基類,提供一些操做節點的方法 */ //===========C# to Xml==========// XmlOperation xmlOperation = new XmlOperation(); //xmlOperation.Create(xmlPath); //xmlOperation.CreateAttribute(xmlPath); //xmlOperation.Delete(xmlPath); //xmlOperation.DeleteAttribute(xmlPath); //xmlOperation.Modify(xmlPath); //xmlOperation.ModifyAttribute(xmlPath); //xmlOperation.Select(xmlPath); //xmlOperation.SelectAttribute(xmlPath); /*=============Linq to Xml===========*/ XmlOperationToLinq xOperation = new XmlOperationToLinq(); // xOperation.Create(xmlPath); /* *1 給指定的XML節點的全部子節點增長一個節點,並增長屬性 *2 刪除指定節點的子節點的指定屬性 *3 */ string lxmlPath = @"F:\XmlTest\test.xml"; xOperation.Create(lxmlPath); xOperation.CreateAttribute(lxmlPath); //xperation.Delete(lxmlPath); //xOperation.DeleteAttribute(lxmlPath); // xOperation.ModifyAttribute(lxmlPath); /*=============C# 讀寫XML===============*/ XmlWriteReadOperation writeRead = new XmlWriteReadOperation(); Console.Read(); } } class XmlOperation { public void Create(string xmlPath) { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(xmlPath); var root = xmlDoc.DocumentElement;//取到根結點 XmlNode newNode = xmlDoc.CreateNode("element", "Name", ""); newNode.InnerText = "Zery"; //添加爲根元素的第一層子結點 root.AppendChild(newNode); xmlDoc.Save(xmlPath); } //屬性 public void CreateAttribute(string xmlPath) { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(xmlPath); XmlElement node = (XmlElement)xmlDoc.SelectSingleNode("Collection/Book"); node.SetAttribute("Name", "C#"); xmlDoc.Save(xmlPath); } public void Delete(string xmlPath) { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(xmlPath); var root = xmlDoc.DocumentElement;//取到根結點 var element = xmlDoc.SelectSingleNode("Collection/Name"); root.RemoveChild(element); xmlDoc.Save(xmlPath); } public void DeleteAttribute(string xmlPath) { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(xmlPath); XmlElement node = (XmlElement)xmlDoc.SelectSingleNode("Collection/Book"); //移除指定屬性 node.RemoveAttribute("Name"); //移除當前節點全部屬性,不包括默認屬性 node.RemoveAllAttributes(); xmlDoc.Save(xmlPath); } public void Modify(string xmlPath) { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(xmlPath); var root = xmlDoc.DocumentElement;//取到根結點 XmlNodeList nodeList = xmlDoc.SelectNodes("/Collection/Book"); //xml不能直接更改結點名稱,只能複製而後替換,再刪除原來的結點 foreach (XmlNode node in nodeList) { var xmlNode = (XmlElement)node; xmlNode.SetAttribute("ISBN", "Zery"); } xmlDoc.Save(xmlPath); } public void ModifyAttribute(string xmlPath) { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(xmlPath); XmlElement element = (XmlElement)xmlDoc.SelectSingleNode("Collection/Book"); element.SetAttribute("Name", "Zhang"); xmlDoc.Save(xmlPath); } public void Select(string xmlPath) { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(xmlPath); //取根結點 var root = xmlDoc.DocumentElement;//取到根結點 //取指定的單個結點 XmlNode singleNode = xmlDoc.SelectSingleNode("Collection/Book"); //取指定的結點的集合 XmlNodeList nodes = xmlDoc.SelectNodes("Collection/Book"); //取到全部的xml結點 XmlNodeList nodelist = xmlDoc.GetElementsByTagName("*"); } public void SelectAttribute(string xmlPath) { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(xmlPath); XmlElement element = (XmlElement)xmlDoc.SelectSingleNode("Collection/Book"); string name = element.GetAttribute("Name"); } } class XmlOperationToLinq { //其它操做 public void OtherOperaton() { //加文件頭 } public void Create(string xmlPath) { XDocument xDoc = XDocument.Load(xmlPath); XElement xElement = xDoc.Element("BookStore"); xElement.Add(new XElement("Test", new XAttribute("Name", "Zery"))); xDoc.Save(xmlPath); } public void CreateAttribute(string xmlPath) { XDocument xDoc = XDocument.Load(xmlPath); IEnumerable<XElement> xElement = xDoc.Element("BookStore").Elements("Book"); foreach (var element in xElement) { element.SetAttributeValue("Name", "Zery"); } xDoc.Save(xmlPath); } public void Delete(string xmlPath) { XDocument xDoc = XDocument.Load(xmlPath); XElement element = (XElement)xDoc.Element("BookStore").Element("Book"); element.Remove(); xDoc.Save(xmlPath); } public void DeleteAttribute(string xmlPath) { XDocument xDoc = XDocument.Load(xmlPath); //不能跨級取節點 XElement element = xDoc.Element("BookStore").Element("Book").Element("Name"); element.Attribute("BookName").Remove(); xDoc.Save(xmlPath); } public void ModifyAttribute(string xmlPath) { XDocument xDoc = XDocument.Load(xmlPath); XElement element = xDoc.Element("BookStore").Element("Book"); element.SetAttributeValue("BookName","ZeryTest"); xDoc.Save(xmlPath); } } internal class XmlWriteReadOperation { } class XmlWriteReadLinqOperation { public void CreatXmlTree(string xmlPath) { XElement xElement = new XElement( new XElement("BookStore", new XElement("Book", new XElement("Name", "C#入門", new XAttribute("BookName", "C#")), new XElement("Author", "Martin", new XAttribute("Name", "Martin")), new XElement("Adress", "上海"), new XElement("Date", DateTime.Now.ToString("yyyy-MM-dd")) ), new XElement("Book", new XElement("Name", "WCF入門", new XAttribute("BookName", "WCF")), new XElement("Author", "Mary", new XAttribute("Name", "Mary")), new XElement("Adress", "北京"), new XElement("Date", DateTime.Now.ToString("yyyy-MM-dd")) ) ) ); //須要指定編碼格式,不然在讀取時會拋:根級別上的數據無效。 第 1 行 位置 1異常 XmlWriterSettings settings = new XmlWriterSettings(); settings.Encoding = new UTF8Encoding(false); settings.Indent = true; XmlWriter xw = XmlWriter.Create(xmlPath,settings); xElement.Save(xw); //寫入文件 xw.Flush(); xw.Close(); } public void WriteXml(string xmlPath) { XElement xElement = new XElement( new XElement("Store", new XElement("Book", "技術類", new XElement("Name", "C#入門", new XAttribute("BookName", "C#")), new XElement("Author", "Martin", new XAttribute("Name", "Zery")), new XComment("如下爲註釋"),//xml註釋 new XElement("Date", DateTime.Now.ToString(), new XAttribute("PublicDate", DateTime.Now.ToString())) )) ); XmlWriter xw = XmlWriter.Create(xmlPath); //保存到XmlWriter xElement.Save(xw); //寫入文件 xw.Flush(); xw.Close(); } } }