30 Days C # foundation Consolidation----Find XML file elements

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.