Xml file operations, xml files

Source: Internet
Author: User

Xml file operations, xml files

Public static XmlDocument getDoc (String path) // load the xml document {XmlDocument doc = new XmlDocument (); doc. load (path); return doc ;} /// <summary> /// return the found node subscript /// </summary> /// <param name = "path"> xml file path </param>/ // <param name = "bname"> title </param> // <returns> </returns> public static int getPosition (String path, string node, String bname) {int I; XmlDocument doc = new XmlDocument (); doc. load (path); XmlNodeList nodeList = doc. selectSingleNode (node ). childNodes; for (I = 0; I <nodeList. count; I ++) {if (nodeList [I]. childNodes [0]. innerText = bname) {return I;} return-1 ;}

1. Traverse

/// <Summary> /// traverse data /// </summary> /// <param name = "sender"> </param> /// <param name = "e"> </param> protected void btnRead_Click (object sender, eventArgs e) {XmlDocument doc = getDoc ("books. xml "); // load the xml document. The books file is stored in foreach (XmlElement root in doc. documentElement. childNodes) // traverse the root node {Response. write ("<script> document. write ('<br/>'); </script> "); Response. write ("<script> document. write ('"+ root. name + "'); </script>"); // node Name root. name book foreach (XmlElement item in root. childNodes) {Response. write ("<script> document. write ('"+ item. name + "'+': '+'" + item. innerText + "'); </script>"); // value of the output node name and text node Response. write ("<script> document. write ('<br/>'); </script> ");}}}

 

2. Search

/// <Summary> /// find /// </summary> /// <param name = "path"> file path </param> /// <param name = "node"> node </param> /// <param name = "bname"> Search Keyword </param> /// <returns> XmlNode </returns> public static xmlNode Find (string path, string node, string bname) {XmlDocument doc = new XmlDocument (); doc. load (path); // Load the xml document XmlNodeList nodeList = doc. selectSingleNode (node ). childNodes; int I = getPosition (path, node, bname); // if (I> = 0) return nodeList [I]; else return null ;}

3. delete a node

/// <Summary> /// delete an element, attribute /// </summary> /// <param name = "path"> file </param> /// <param name = "node"> specify the parent node of the node </param> // <param name = "attribute"> if it is null, delete the node, otherwise, Delete the attribute </param> /// <param name = "bname"> </param> public static void Delete (string path, string node, string attribute, string bname) {XmlDocument doc = new XmlDocument (); doc. load (path); XmlNode root = doc. selectSingleNode (node); XmlNodeList nodeList = doc. selectSingleNode (node ). childNodes; int I = getPosition (path, node, bname); // return the specified node subscript if (I> = 0) {if (attribute. equals ("") {root. removeChild (nodeList [I]);} else {XmlElement xn = (XmlElement) nodeList [I]; xn. removeAttribute (attribute) ;}} doc. save (path );}

4. Add

/// <Summary> /// add element value /// </summary> /// <param name = "path"> </param> /// <param name = "node"> </param> // <param name = "element"> </param> // <param name = "value"> </param>/ // <param name = "I"> insert subscript, if it is negative, the last node is inserted by default </param> /// <returns> </returns> public static bool Add (string path, string node, string element, string value, int I) {XmlDocument doc = new XmlDocument (); doc. load (path); XmlNodeList nodeList = doc. selectNodes (node); XmlNode newNode = doc. selectSingleNode (node ). lastChild; if (I <0 | I> nodeList. count-1) // if it is less than 0 or greater than the node length, {XmlElement newElement = doc is added from the last node by default. createElement (element); // create the element newElement. innerText = value; // value newNode. appendChild (newElement);} else {XmlElement newElement = doc. createElement (element); newElement. innerText = value; nodeList [I-1]. appendChild (newElement);} doc. save (path); return true ;}

 


C #. Operations on xml files

File Path:... bin \ Debug \ School. xml
<? Xml version = "1.0" encoding = "UTF-8"?>
<Class>
<Student Id = "1">
<Age> 20 </Age>
<Name> AAA </Name>
</Student>
<Student Id = "2">
<Age> 22 </Age>
<Name> BBB </Name>
</Student>
</Class>
---------------------------------------------------
XmlDocument doc = new XmlDocument ();
Doc. Load ("School. xml ");
XmlElement root = doc. DocumentElement;
Foreach (XmlNode item in root. ChildNodes)
{

If (item. Attributes ["Id"]. Value = "1 ")
{

Item ["Age"]. InnerText = "19 ";
Item ["Name"]. InnerText = "CCC ";
}
If (item. Attributes ["Id"]. Value = "2 ")
{

Item ["Age"]. InnerText = "23 ";
Item ["Name"]. InnerText = "18 ";
}
}
Doc. Save ("School. xml ");

XML file size

The advantages of SAX processing are very similar to those of streaming media. The analysis can start immediately, rather than waiting for all data to be processed. In addition, because the application only checks data when reading data, it does not need to store the data in the memory. This is a huge advantage for large documents. In fact, the application does not even have to parse the entire document; it can stop parsing when a condition is met. In general, SAX is much faster than its replacement DOM.

Select DOM or SAX? For developers who need to write their own code to process XML documents, choosing DOM or the SAX Parsing Model is a very important design decision. DOM uses a tree structure to access XML documents, while SAX uses an event model.

The DOM parser converts an XML document into a tree containing its content and can traverse the tree. The advantage of using DOM to parse the model is that programming is easy. Developers only need to call the build instruction and then use navigation APIs to access the desired Tree node to complete the task. You can easily add and modify elements in the tree. However, because the DOM parser needs to process the entire XML file, the performance and memory requirements are high, especially when a large XML file is encountered. Due to its traversal capability, DOM parser is often used in services that require frequent changes in XML documents.

The SAX Parser uses an event-based model. It triggers a series of events when parsing XML documents. When a given tag is found, it can activate a callback method, tell the method that the label has been found. The memory requirements of SAX are usually relatively low, because it allows developers to decide the tag to be processed by themselves. especially when developers only need to process part of the data contained in the document, the extension capability of SAX is better reflected. However, it is difficult to use the SAX Parser to encode data, and it is difficult to access multiple different data in the same document at the same time.

Related Article

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.