C # XmlHelper help class for operating Xml documents,

Source: Internet
Author: User

C # XmlHelper help class for operating Xml documents,

Using System. xml; using System. data; namespace DotNet. utilities {// <summary> /// public class for Xml operations /// </summary> public class XmlHelper {# region field definition /// <summary> /// physical path of the XML file // </summary> private string _ filePath = string. empty; // <summary> // Xml document // </summary> private XmlDocument _ xml; /// <summary> /// XML root node // </summary> private XmlElement _ element; # endregion # region constructor // <summary> /// Instantiate the XmlHelper object // </summary> /// <param name = "xmlFilePath"> relative path of the Xml file </param> public XmlHelper (string xmlFilePath) {// obtain the absolute path of the XML file _ filePath = SysHelper. getPath (xmlFilePath );} # endregion # region create the XML root node /// <summary> /// create the XML root node /// </summary> private void CreateXMLElement () {// create an XML object _ xml = new XmlDocument (); if (DirFile. isExistFile (_ filePath) {// load the XML file _ xml. load (this. _ filePat H);} // assign _ element = _ XML to the xml root node. documentElement ;} # endregion # region get the Node object of the specified XPath expression /// <summary> /// get the Node object of the specified XPath expression /// </summary> /// <param name = "xPath"> XPath expressions, /// Example 1: @ "Skill/First/SkillItem", equivalent to @ "// Skill/First/SkillItem" // Example 2: @ "Table [USERNAME = 'a']", [] indicates filtering. USERNAME is a subnode in Table. /// Example 3: @ "ApplyPost/Item [@ itemName = 'Post number']", @ itemName is the attribute of the Item node. /// </param> public XmlNode GetNode (string xPath) {// create the XML root node CreateXMLElement (); // return the XPath node return _ element. selectSingleNode (xPath );} # endregion # region obtain the value of the node of the specified XPath expression /// <summary> /// obtain the value of the node of the specified XPath expression /// </summary> /// <param name = "xPath"> XPath expressions, /// Example 1: @ "Skill/First/SkillItem", equivalent to @ "// Skill/First/SkillItem" // Example 2: @ "Table [USERNAME = 'a']", [] indicates filtering. USERNAME is a subnode in Table. /// Example 3: @ "ApplyPost/Item [@ itemName = 'post '] ", @ ItemName is the attribute of the Item node. /// </param> public string GetValue (string xPath) {// create the XML root node CreateXMLElement (); // return the value of the XPath node return _ element. selectSingleNode (xPath ). innerText ;} # endregion # region get the attribute value of the node of the specified XPath expression /// <summary> /// get the attribute value of the node of the specified XPath expression /// </summary> /// <param name = "xPath"> XPath expressions, /// Example 1: @ "Skill/First/SkillItem", equivalent to @ "// Skill/First/SkillItem" // Example 2: @ "Table [USERNAME = 'a']", [] Indicates filtering. USERNAME is a subnode in the Table. /// Example 3: @ "ApplyPost/Item [@ itemName = 'Post number']", @ itemName is the attribute of the Item node. /// </param> /// <param name = "attributeName"> attribute name </param> public string GetAttributeValue (string xPath, string attributeName) {// create the root node CreateXMLElement () for XML; // return the attribute value of the XPath node return _ element. selectSingleNode (xPath ). attributes [attributeName]. value ;}# endregion # Add a node to region /// <summary> /// 1. function: adds a node.. /// 2. usage condition: insert any node into the current Xml file. /// </Summary> /// <param name = "xmlNode"> Xml node to be inserted </param> public void AppendNode (XmlNode xmlNode) {// create the root node CreateXMLElement () for XML; // import the XmlNode node = _ xml. importNode (xmlNode, true); // Insert the node to the _ element under the root node. appendChild (node);} // <summary> // 1. function: adds a node. /// 2. usage condition: insert the first record in DataSet into the Xml file. /// </Summary> /// <param name = "ds"> DataSet instance. The DataSet must have only one record </param> public void AppendNode (DataSet ds) {// create XmlDataDocument object XmlDataDocument xmlDataDocument = new XmlDataDocument (ds); // import node XmlNode node = xmlDataDocument. documentElement. firstChild; // Insert the node to the AppendNode (node) under the root node );} # endregion # region delete a node /// <summary> /// Delete the node of the specified XPath expression /// </summary> /// <param name = "xPath"> XPath expression, /// Example 1: @ "Skill/First/SkillItem", equivalent to @ "// Skill/First/SkillItem" // Example 2: @ "Table [USERNAME = 'a']", [] indicates filtering. USERNAME is a subnode in Table. /// Example 3: @ "ApplyPost/Item [@ itemName = 'Post number']", @ itemName is the attribute of the Item node. /// </param> public void RemoveNode (string xPath) {// create the XML root node CreateXMLElement (); // obtain the node XmlNode node to be deleted = _ xml. selectSingleNode (xPath); // delete node _ element. removeChild (node );} # endregion // delete node # region Save the XML file // <summary> // Save the XML file /// </summary> public void Save () {// create the root node CreateXMLElement () for XML; // Save the XML file _ xml. save (this. _ filePath );} # endregion // Save the XML file # region static method # region creates a Root Node object // <summary> // create a Root Node object // </summary> /// <param name = "xmlFilePath"> relative path of the Xml file </param> private static XmlElement CreateRootElement (string xmlFilePath) {// defines the variable, indicating the absolute path of the XML file string filePath = ""; // gets the absolute path of the XML file filePath = SysHelper. getPath (xmlFilePath); // create the XmlDocument object XmlDocument xmlDocument = new XmlDocument (); // load the XML file xmlDocument. load (filePath); // return the root node return xmlDocument. documentElement ;} # endregion # region obtain the value of the node of the specified XPath expression /// <summary> /// obtain the value of the node of the specified XPath expression /// </summary> /// <param name = "xmlFilePath"> relative path of the Xml file </param> // <param name = "xPath"> XPath expression, /// Example 1: @ "Skill/First/SkillItem", equivalent to @ "// Skill/First/SkillItem" // Example 2: @ "Table [USERNAME = 'a']", [] indicates filtering. USERNAME is a subnode in Table. /// Example 3: @ "ApplyPost/Item [@ itemName = 'Post number']", @ itemName is the attribute of the Item node. /// </param> public static string GetValue (string xmlFilePath, string xPath) {// create the root object XmlElement rootElement = CreateRootElement (xmlFilePath ); // return the value of the XPath node return rootElement. selectSingleNode (xPath ). innerText ;} # endregion # region get the attribute value of the node of the specified XPath expression /// <summary> /// get the attribute value of the node of the specified XPath expression /// </summary> /// <param name = "xmlFilePath"> relative path of the Xml file </param> // <param name = "xPath"> XPath expression, /// Example 1: @ "Skill/First/SkillItem", equivalent to @ "// Skill/First/SkillItem" // Example 2: @ "Table [USERNAME = 'a']", [] indicates filtering. USERNAME is a subnode in Table. /// Example 3: @ "ApplyPost/Item [@ itemName = 'Post number']", @ itemName is the attribute of the Item node. /// </param> /// <param name = "attributeName"> attribute name </param> public static string GetAttributeValue (string xmlFilePath, string xPath, string attributeName) {// create the root object XmlElement rootElement = CreateRootElement (xmlFilePath); // return the attribute value of the XPath node return rootElement. selectSingleNode (xPath ). attributes [attributeName]. value ;}# endregion # endregion public static void SetValue (string xmlFilePath, string xPath, string newtext) {// string path = SysHelper. getPath (xmlFilePath); // var queryXML = from xmlLog in xelem. descendants ("msg_log") // All records whose names are Bin // where xmlLog. element ("user "). value = "Bin" // select xmlLog; // foreach (XElement el in queryXML) // {// el. element ("user "). value = "LiuBin"; // start to modify //} // xelem. save (path );}}}

 


In C language-> what?

-> Is a whole. It is used to point to a struct, class in C ++, and other pointers containing sub-data to obtain sub-data. In other words, if we define a struct in C and declare a pointer pointing to this struct, we need to use "->" to retrieve the data in the struct using the pointer ".
For example:
Struct Data
{
Int a, B, c;
};/* Define struct */
Struct Data * p;/* define struct pointer */
Struct Data A = {1, 2, 3};/* declare variable */
Int x;/* declare a variable x */
P = & A;/* point p to */
X = p-> a;/* indicates that the data item a in the struct pointed to by p is assigned to x */
/* Because p points to A, p-> a = A. a, that is, 1 */

For the first problem, p = p-> next; this should appear in the linked list of C language. next here should be a struct pointer of the same type as p, and its definition format should be:
Struct Data
{
Int;
Struct Data * next;
};/* Define struct */
............
Main ()
{
Struct Data * p;/* declare the pointer Variable p */
......
P = p-> next;/* assign the value in next to p */
}
The linked list pointer is a difficulty in C language, but it is also the key. It is very useful to learn it. To be careful, you must first talk about variables and pointers.
What is a variable? The so-called variables should not be simply thought that the amount will become a variable. Let's use the question of our Dean: "Is the classroom changing ?" Change, because there are different people in the classroom every day, but they do not change, because the classroom is always there, and it does not become larger or smaller. This is the variable: There is a constant address and a variable storage space. Under normal circumstances, we only see the variable in the room, that is, its content, but do not pay attention to the variable address, but the C language pointer is the address of the room. We declare that variables are equivalent to building a house to store things. We can directly watch things in the house, while declaring pointers is equivalent to getting a positioner. When a pointer points to a variable, it is to use the pointer to locate the variable. Then we can use the pointer to find the variable "tracked" and get the content in it.
What about struct? The structure is equivalent to a villa composed of several houses, and several houses are bound for use together. Suppose there are many such villas distributed in a big maze, and each villa has a house. The location information of another villa is put in it. Now you have found the first villa with the positioner and obtained what you want from it (the data part of the linked list ), then, calculate the location of the next villa into your positioner (p = p-> next), and go down to the next villa ...... If you go on like this, you will know that the information of a villa on the ground is gone (p-> next = NULL), and your trip is over. This is the process of traversing a linked list. Now you can understand the meaning of p = p-> next!
Write so much. I hope you can understand.
If you want to learn c and C ++ well, you must be familiar with linked lists and pointers!

In C language-> what?

-> Is a whole. It is used to point to a struct, class in C ++, and other pointers containing sub-data to obtain sub-data. In other words, if we define a struct in C and declare a pointer pointing to this struct, we need to use "->" to retrieve the data in the struct using the pointer ".
For example:
Struct Data
{
Int a, B, c;
};/* Define struct */
Struct Data * p;/* define struct pointer */
Struct Data A = {1, 2, 3};/* declare variable */
Int x;/* declare a variable x */
P = & A;/* point p to */
X = p-> a;/* indicates that the data item a in the struct pointed to by p is assigned to x */
/* Because p points to A, p-> a = A. a, that is, 1 */

For the first problem, p = p-> next; this should appear in the linked list of C language. next here should be a struct pointer of the same type as p, and its definition format should be:
Struct Data
{
Int;
Struct Data * next;
};/* Define struct */
............
Main ()
{
Struct Data * p;/* declare the pointer Variable p */
......
P = p-> next;/* assign the value in next to p */
}
The linked list pointer is a difficulty in C language, but it is also the key. It is very useful to learn it. To be careful, you must first talk about variables and pointers.
What is a variable? The so-called variables should not be simply thought that the amount will become a variable. Let's use the question of our Dean: "Is the classroom changing ?" Change, because there are different people in the classroom every day, but they do not change, because the classroom is always there, and it does not become larger or smaller. This is the variable: There is a constant address and a variable storage space. Under normal circumstances, we only see the variable in the room, that is, its content, but do not pay attention to the variable address, but the C language pointer is the address of the room. We declare that variables are equivalent to building a house to store things. We can directly watch things in the house, while declaring pointers is equivalent to getting a positioner. When a pointer points to a variable, it is to use the pointer to locate the variable. Then we can use the pointer to find the variable "tracked" and get the content in it.
What about struct? The structure is equivalent to a villa composed of several houses, and several houses are bound for use together. Suppose there are many such villas distributed in a big maze, and each villa has a house. The location information of another villa is put in it. Now you have found the first villa with the positioner and obtained what you want from it (the data part of the linked list ), then, calculate the location of the next villa into your positioner (p = p-> next), and go down to the next villa ...... If you go on like this, you will know that the information of a villa on the ground is gone (p-> next = NULL), and your trip is over. This is the process of traversing a linked list. Now you can understand the meaning of p = p-> next!
Write so much. I hope you can understand.
If you want to learn c and C ++ well, you must be familiar with linked lists and pointers!

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.