using System; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Ink; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; using System.Xml.Linq; namespace LinktoXml.Class { /* * Linq to Xml Tree Structure * XAttribute :该类表示一个XML节点的属性 * XElement :表示一个Xml 元素,常用格式为<element>....</element> <BookList> <book name="english" type="education"> <!--name: XAttribute --> <author>zhangweia</author> <!-- XElement--> <price>20</price> </book> <book name="math" type="education"> <author>zhangweib</author> <price>30</price> </book> </BookList> */ public class CreateXmlTree { #region XElement /* public class CreateXElementWays { /// <summary> /// create a node /// </summary> /// <param name="name">node label name</param> private void XElement(XName name); /// <summary> /// create a node /// </summary> /// <param name="element"></param> public void XElement(XElement element); /// <summary> /// /// </summary> /// <param name="name"></param> /// <param name="content"></param> public void XElement(XName name,object content); //param is object list public void XElement(XName name,params object[] content); } */ public void CreateElement() { XElement root = new XElement("BookList"); // <BookList></BookList> XElement bookE = new XElement("book"); // <book></book> XElement authorE = new XElement("author", "zhangweia"); //<author>zhangweia</author> object is String XElement priceE = new XElement("price", "20"); //<price>20</price> /* <book name="english"> <author>zhangweia</author> <price>20</price> </book> */ XElement firstNode = new XElement("book", //对象为多个子节点 authorE, priceE ); XElement secondNode = new XElement("book", // XElement(XName name,params object[] content); object is XElement new XElement("author", "zhangweib"), new XElement("price", "30") ); XElement XmlTree = new XElement("BookList", firstNode, secondNode); MessageBox.Show(XmlTree.ToString()); } #endregion #region XAttribute /* public class CreateAttribute { public void XAttribute(XName name, object value); public void XAttribute(XAttribute otehr); } */ // <book name="english" type="education"/> public void CreateAttributeNode() { XAttribute nameA = new XAttribute("name","english"); XAttribute typeA = new XAttribute("type", "education"); //<book name="english" type="education"/> XElement bookE = new XElement("book", //XElement(XName name,params object[] content); object is XAttribute nameA, typeA); MessageBox.Show(bookE.ToString()); } public void CreateXmlByAttributeAndElement() { XElement root = new XElement("BookList", new XElement("book", new XAttribute("name","english"), new XAttribute("type","education"), new XElement("author","zhangweia"), new XElement("price","20")), new XElement("book", new XAttribute("name","math"), new XAttribute("type","education"), new XElement("author","zhangweib"), new XElement("price","30"))); MessageBox.Show(root.ToString()); } #endregion } }
CreateElement 方法生成的XML如图:
CreateAttributeNode 方法生成的XML如图:
CreateXmlByAttributeAndElement 方法生成的XML如图: