C # Summary of XML operation methods,

Source: Internet
Author: User

C # Summary of XML operation methods,

Using System. Xml;
Using System. Data;

Namespace DotNet. Utilities
{
/// <Summary>
/// Common Xml operation class
/// </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. _ filePath );
}

// Assign values to the XML Root Node
_ Element = _ xml. DocumentElement;
}
# Endregion

# Region get the Node object of the specified XPath expression
/// <Summary>
/// Obtain the Node object 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 XmlNode GetNode (string xPath)
{
// Create the XML Root Node
CreateXMLElement ();

// Return the XPath Node
Return _ element. SelectSingleNode (xPath );
}
# Endregion

# Region obtain the node value of the specified XPath expression
/// <Summary>
/// Obtain the node value 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 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>
/// Obtain the attribute value of 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>
/// <Param name = "attributeName"> attribute name </param>
Public string GetAttributeValue (string xPath, string attributeName)
{
// Create the XML Root Node
CreateXMLElement ();

// Return the attribute value of the XPath Node
Return _ element. SelectSingleNode (xPath). Attributes [attributeName]. Value;
}
# Endregion

# Add a node to region
/// <Summary>
/// 1. function: Add 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 XML Root Node
CreateXMLElement ();

// Import nodes
XmlNode node = _ xml. ImportNode (xmlNode, true );

// Insert the node to the root node
_ Element. AppendChild (node );
}

/// <Summary>
/// 1. function: Add a node.
/// 2. usage condition: insert the first record in DataSet into the Xml file.
/// </Summary>
/// <Param name = "ds"> DataSet instance, This DataSet should have only one record </param>
Public void AppendNode (DataSet ds)
{
// Create an XmlDataDocument object
XmlDataDocument xmlDataDocument = new XmlDataDocument (ds );

// Import nodes
XmlNode node = xmlDataDocument. DocumentElement. FirstChild;

// Insert the node to the root node
AppendNode (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 to be deleted
XmlNode node = _ xml. SelectSingleNode (xPath );

// Delete a node
_ Element. RemoveChild (node );
}
# Endregion // delete a node

# Region saving XML files
/// <Summary>
/// Save the XML file
/// </Summary>
Public void Save ()
{
// Create the XML Root Node
CreateXMLElement ();

// Save the XML file
_ Xml. Save (this. _ filePath );
}
# Endregion // Save the XML file

# Region static method

# Region create 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)
{
// Define the variable, indicating the absolute path of the XML file
String filePath = "";

// Obtain the absolute path of the XML file
FilePath = SysHelper. GetPath (xmlFilePath );

// Create an XmlDocument object
XmlDocument xmlDocument = new XmlDocument ();
// Load the XML file
XmlDocument. Load (filePath );

// Return the root node
Return xmlDocument. DocumentElement;
}
# Endregion

# Region obtain the node value of the specified XPath expression
/// <Summary>
/// Obtain the node value 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>
/// Obtain 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 modification
//}
// Xelem. Save (path );
}
}
}

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.