Using System;
Using System. Collections. Generic;
Using System. Text;
Using System. Xml;
Using System. Data;
Using System. IO;
/** // <Summary>
/// XML operation base class
/// </Summary>
Public class XmlHelper
{
/** // <Summary>
/// Read Xml to DataSet
/// </Summary>
/// <Param name = "XmlPath"> path </param>
/// <Returns> result set </returns>
Public static DataSet GetXml (string XmlPath)
{
DataSet ds = new DataSet ();
Ds. ReadXml (@ XmlPath );
Return ds;
}
/** // <Summary>
/// Read the xml document and return a node: Applicable to level 1 nodes
/// </Summary>
/// <Param name = "XmlPath"> xml Path </param>
/// <Param name = "NodeName"> node </param>
/// <Returns> </returns>
Public static string ReadXmlReturnNode (string XmlPath, string Node)
{
XmlDocument docXml = new XmlDocument ();
DocXml. Load (@ XmlPath );
XmlNodeList xn = docXml. GetElementsByTagName (Node );
Return xn. Item (0). InnerText. ToString ();
}
/** // <Summary>
/// Query data, return all the lower-level nodes of the current node, and fill in a DataSet
/// </Summary>
/// <Param name = "xmlPath"> xml document path </param>
/// <Param name = "XmlPathNode"> node path: Root Node/parent node/current node </param>
/// <Returns> </returns>
Public static DataSet GetXmlData (string xmlPath, string XmlPathNode)
{
XmlDocument objXmlDoc = new XmlDocument ();
ObjXmlDoc. Load (xmlPath );
DataSet ds = new DataSet ();
StringReader read = new StringReader (objXmlDoc. SelectSingleNode (XmlPathNode). OuterXml );
Ds. ReadXml (read );
Return ds;
}
/** // <Summary>
/// Update the Xml node content
/// </Summary>
/// <Param name = "xmlPath"> xml Path </param>
/// <Param name = "Node"> Node to be replaced: Node path root Node/parent Node/current Node </param>
/// <Param name = "Content"> new Content </param>
Public static void XmlNodeReplace (string xmlPath, string Node, string Content)
{
XmlDocument objXmlDoc = new XmlDocument ();
ObjXmlDoc. Load (xmlPath );
ObjXmlDoc. SelectSingleNode (Node). InnerText = Content;
ObjXmlDoc. Save (xmlPath );
}
/** // <Summary>
/// Modify the node Attribute Value
/// </Summary>
/// <Param name = "xmlPath"> file path </param>
/// <Param name = "NodePath"> node path </param>
/// <Param name = "NodeAttribute1"> name of the node attribute to be changed </param>
/// <Param name = "NodeAttributeText"> changed attribute value </param>
Public static void XmlAttributeEdit (string xmlPath, string NodePath, string NodeAttribute1, string NodeAttributeText)
{
XmlDocument objXmlDoc = new XmlDocument ();
ObjXmlDoc. Load (xmlPath );
XmlNode nodePath = objXmlDoc. SelectSingleNode (NodePath );
XmlElement xe = (XmlElement) nodePath;
Xe. SetAttribute (NodeAttribute1, NodeAttributeText );
ObjXmlDoc. Save (xmlPath );
}
/** // <Summary>
/// Delete the XML node and Its subnodes
/// </Summary>
/// <Param name = "xmlPath"> xml document path </param>
/// <Param name = "Node"> Node path </param>
Public static void XmlNodeDelete (string xmlPath, string Node)
{
XmlDocument objXmlDoc = new XmlDocument ();
ObjXmlDoc. Load (xmlPath );
String mainNode = Node. Substring (0, Node. LastIndexOf ("/"));
ObjXmlDoc. SelectSingleNode (mainNode). RemoveChild (objXmlDoc. SelectSingleNode (Node ));
ObjXmlDoc. Save (xmlPath );
}
/** // <Summary>
/// Delete the attributes of a node
/// </Summary>
/// <Param name = "xmlPath"> file path </param>
/// <Param name = "NodePath"> node path (xpath) </param>
/// <Param name = "NodeAttribute"> attribute name </param>
Public static void xmlnNodeAttributeDel (string xmlPath, string NodePath, string NodeAttribute)
{
XmlDocument objXmlDoc = new XmlDocument ();
ObjXmlDoc. Load (xmlPath );
XmlNode nodePath = objXmlDoc. SelectSingleNode (NodePath );
XmlElement xe = (XmlElement) nodePath;
Xe. RemoveAttribute (NodeAttribute );
ObjXmlDoc. Save (xmlPath );
}
/** // <Summary>
/// Insert a node and Its subnodes
/// </Summary>
/// <Param name = "xmlPath"> xml Path </param>
/// <Param name = "MailNode"> current Node path </param>
/// <Param name = "ChildNode"> insert a node </param>
/// <Param name = "Element"> insert a subnode of a node </param>
/// <Param name = "Content"> subnode Content </param>
Public static void XmlInsertNode (string xmlPath, string MailNode, string ChildNode, string Element, string Content)
{
XmlDocument objXmlDoc = new XmlDocument ();
ObjXmlDoc. Load (xmlPath );
XmlNode objRootNode = objXmlDoc. SelectSingleNode (MailNode );
XmlElement objChildNode = objXmlDoc. CreateElement (ChildNode );
ObjRootNode. AppendChild (objChildNode );
XmlElement objElement = objXmlDoc. CreateElement (Element );
ObjElement. InnerText = Content;
ObjChildNode. AppendChild (objElement );
ObjXmlDoc. Save (xmlPath );
}
/** // <Summary>
/// Add attributes to a node
/// </Summary>
/// <Param name = "xmlPath"> xml file path </param>
/// <Param name = "NodePath"> node path </param>
/// <Param name = "NodeAttribute1"> name of the node attribute to be added </param>
/// <Param name = "NodeAttributeText"> value of the attribute to be added </param>
Public static void AddAttribute (string xmlPath, string NodePath, string NodeAttribute1, string NodeAttributeText)
{
XmlDocument objXmlDoc = new XmlDocument ();
ObjXmlDoc. Load (xmlPath );
XmlAttribute nodeAttribute = objXmlDoc. CreateAttribute (NodeAttribute1 );
XmlNode nodePath = objXmlDoc. SelectSingleNode (NodePath );
NodePath. Attributes. Append (nodeAttribute );
XmlElement xe = (XmlElement) nodePath;
Xe. SetAttribute (NodeAttribute1, NodeAttributeText );
ObjXmlDoc. Save (xmlPath );
}
/** // <Summary>
/// Insert a node with a property
/// </Summary>
/// <Param name = "xmlPath"> Xml document path </param>
/// <Param name = "MainNode"> current Node path </param>
/// <Param name = "Element"> new node </param>
/// <Param name = "Attrib"> attribute name </param>
/// <Param name = "AttribContent"> attribute value </param>
/// <Param name = "Content"> new node value </param>
Public static void XmlInsertElement (string xmlPath, string MainNode, string Element, string Attrib, string AttribContent, string Content)
{
XmlDocument objXmlDoc = new XmlDocument ();
ObjXmlDoc. Load (xmlPath );
XmlNode objNode = objXmlDoc. SelectSingleNode (MainNode );
XmlElement objElement = objXmlDoc. CreateElement (Element );
ObjElement. SetAttribute (Attrib, AttribContent );
ObjElement. InnerText = Content;
ObjNode. AppendChild (objElement );
ObjXmlDoc. Save (xmlPath );
}
/** // <Summary>
/// Insert a node without attributes
/// </Summary>
/// <Param name = "xmlPath"> Xml document path </param>
/// <Param name = "MainNode"> current Node path </param>
/// <Param name = "Element"> new node </param>
/// <Param name = "Content"> new node value </param>
Public static void XmlInsertElement (string xmlPath, string MainNode, string Element, string Content)
{
XmlDocument objXmlDoc = new XmlDocument ();
ObjXmlDoc. Load (xmlPath );
XmlNode objNode = objXmlDoc. SelectSingleNode (MainNode );
XmlElement objElement = objXmlDoc. CreateElement (Element );
ObjElement. InnerText = Content;
ObjNode. AppendChild (objElement );
ObjXmlDoc. Save (xmlPath );
}
/** // <Summary>
/// Add a parent node under the root node
/// </Summary>
Public static void AddParentNode (string xmlPath, string parentNode)
{
XmlDocument xdoc = new XmlDocument ();
Xdoc. Load (xmlPath );
// Create a new menber node and add it to the root node
XmlElement Node = xdoc. CreateElement (parentNode );
Xdoc. DocumentElement. PrependChild (Node );
Xdoc. Save (xmlPath );
}
/** // <Summary>
/// Read the subnode value based on the node attribute (compared with the Provincial Resource Mode)
/// </Summary>
/// <Param name = "XmlPath"> xml Path </param>
/// <Param name = "FatherElement"> parent node value </param>
/// <Param name = "AttributeName"> attribute name </param>
/// <Param name = "AttributeValue"> attribute value </param>
/// <Param name = "ArrayLength"> returned array length </param>
/// <Returns> </returns>
Public static System. Collections. ArrayList GetSubElementByAttribute (string XmlPath, string FatherElement, string AttributeName, string AttributeValue, int ArrayLength)
{
System. Collections. ArrayList al = new System. Collections. ArrayList ();
XmlDocument docXml = new XmlDocument ();
DocXml. Load (@ XmlPath );
XmlNodeList xn;
Xn = docXml. DocumentElement. SelectNodes ("//" + FatherElement + "[" + @ AttributeName + "= '" + AttributeValue + "']");
XmlNodeList xx = xn. Item (0). ChildNodes;
For (int I = 0; I <ArrayLength & I <xx. Count; I ++)
{
Al. Add (xx. Item (I). InnerText );
}
Return al;
}
}