One: XML document
Learn about XML documents.
Use code to create an XML document.
Reference namespace +using System.Xml;
XmlDocument xdoc=new XmlDocument (); XmlDeclaration Xdec = Xdoc. Createxmldeclaration ("1.0", "gb2312", null); Set the version information Xdoc. AppendChild (XDEC); Set node root XmlElement Xele = Xdoc. createelement ("root"); Xdoc. AppendChild (Xele); Sets the attribute ID of the node; XmlAttribute Xatt = Xdoc. CreateAttribute ("id"); Xatt. Value = "Ahui"; Id=ahui; Sets the text content of the node. XmlText xttext = Xdoc. createTextNode ("aaaaaaaa"); Xele. AppendChild (xttext); The text is added below the root node. Xele. Attributes.append (Xatt); The attribute ID is added under the root property. Xdoc. Save ("1.xml"); Save the XML file.
---->LINQ to XML
Reference namespace, using System.Xml.Linq; XDocument xdoc=new XDocument (); XElement xele=new XElement ("root", "Hello, World"); XAttribute xatt=new XAttribute ("id", "002"); Xdoc.add (Xele); Xele. ADD (Xatt); Xdoc.save ("2.xml");
Find--->xml
Find Name= "Zhang Hui 01" from the XML text and return to the parent node.
Class Program {static void Main (string[] args) {#region Lookup XML//read local XML file. XDocument Xdoc = xdocument.load ("Ahui.xml"); List<xelement> xele=new list<xelement> (); Searchelementszhanhui (Xdoc. Root,xele); foreach (XElement item in Xele) {Console.WriteLine (item. Name); } console.readkey (); #endregion}///<summary>//Find name= Zhang Hui 01 in the XML document. </summary>//<param name= "ele" ></param>//<param name= "list" ></param> public static void Searchelementszhanhui (XElement ele,list<xelement> List) {//Iterates through all sections of Ele first Point//ele. Elements () + returns the set of child elements of this element or document in document order, foreach (XElement item in Ele. Elements ()) {//Determines whether this element is Zhang Hui if (item). name.localname== "Name") { if (item. Value = = "Zhang Hui") {list. ADD (item. Parent); }}//If the item has a child node in it, it is recursive. Searchelementszhanhui (item,list); } } }
30 Days C # foundation Consolidation----Find XML file elements