. Net Read XML

Source: Internet
Author: User
Tags httpcontext processing instruction

I. GENERAL methods

1. Introduction to Knowledge

      Initializes an XML object        XmlDocument XML = new XmlDocument ();        Loads the XML file        XML. Load ("File path");        Reads the specified node        XmlNode XmlNode = XML. selectSingleNode ("/node name");        Determine if there are sub-nodes under the node        xmlnode.haschildnodes;        Read all sub-nodes under node        xmlnodelist XmlNodeList = xmlnode.childnodes;        Reads a list of multiple nodes with the same name        xmlnodelist XmlNodeList = XML. SelectNodes ("/root/node name");        Read the properties of the node        string attribute = xmlnode.attributes["property name"]. Value;        Read the text of the node        string value = Xmlnode.innertext;        Create a new node        XmlNode XmlNode = XML. CreateElement ("node name");        Save the XML file XML        . Save ("File path");

2. Example

        Creates and initializes an XML object        XmlDocument XML = new XmlDocument ();        Load the XML file in the        try        {            XML. Load (HttpContext.Current.Server.MapPath ("~/datafunc/allmana.xml"));        }        catch (Exception)        {            throw new exceptionmsg (4, "Failed to load XML file!) ");         }        Read node        XmlNode snxmlnode = XML. selectSingleNode ("/config/systemname");        SystemName = Snxmlnode.innertext;

Second, LINQ reads XML files

1. Introduction to Knowledge

A.xdocument, which is one of the common LINQ to XML classes, provides methods for working with XML documents, including declaring and annotating each processing instruction.

B.xelement it represents an XML element that can be used to create elements, change element content, add, change, delete child elements, add attributes to an element, or serialize the content of an element in text format.

C.xattribute It is used to manipulate the attributes of an element

2. Example

Creating an XML file

Create the XDocument object and instantiate the object        XDocument xdoc = new XDocument (            //xml the rasterize and encode            new Xdeclaration ("1.0", "Utf-8", "Yes" ),            //Create element            new XElement ("Config",            //create element, and make child element of previous element                new XElement ("System",                //Create element's properties                new XAttribute ("Name", "Allmana"),                new XElement ("StoreName", "Provincial Maternity Shop")                ,            //create sibling element            new XElement (" System ",                new XAttribute (" Name "," Allmana1 "),                new XElement (" StoreName "," City Maternity and Child Shop ")))                ;        Save the XML file        xdoc. Save (HttpContext.Current.Server.MapPath ("~/dataconfig/config.xml"));

. Net Read XML

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.