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