XML Operations Class (Seaskyer)

Source: Internet
Author: User
Tags xpath
Using System;
Using System.Collections;
Using System.Data;
Using System.Text;
Using System.Xml;
Using Seaskyer.fso;
Using Seaskyer.strings;

Namespace Seaskyer.xml
{
<summary>
Operatexml: An operation on an Xml file.
</summary>
<remarks>
XPath description
///  /*
* Query properties: (//sectionname[@propertyName =value])
* Query child nodes: (//sectionname[childsectionname=value])
* Layer-by-step inquiry: (//root/sectionname/child ...)
///   */
</remarks>
public class Operatexml
{
<summary>
Public constructors
</summary>
Public Operatexml ()
{
}

<summary>
Public constructors
</summary>
<param name= "path" > XML file absolute path </param>
Public Operatexml (String Path)
{
this. FilePath = Path;
}

Private Field
private string _filecontent;
private string _filepath;
private string _xpath;
Private Operatexmlmethod _method;


#region Public Property
<summary>
Content in XML format
</summary>
public string Filecontent
{
get {return _filecontent;}
set {_filecontent = value;}
}
<summary>
Absolute path to XML file
</summary>
public string FilePath
{
get {return _filepath;}
set {_filepath = value;}
}
<summary>
An XPath expression
</summary>
public string XPath
{
get {return _xpath;}
set {_xpath = value;}
}
<summary>
Mode of operation
</summary>
Public Operatexmlmethod method
{
get {return _method;}
set {_method = value;}
}
#endregion




# region "XML file Operations"

#region "New"
<summary>
Creates a new, blank, XML-formatted file (no root node)
</summary>
public void Createxml ()
{
Create a temporary file with the same name to ensure that the folder exists
Fobject.create (this. FilePath, fsomethod.file);

Create a XmlTextWriter instance
XmlTextWriter writer = new XmlTextWriter (this. FilePath, encoding.getencoding ("gb2312"));
Writer. formatting = formatting.indented;
Writer. indentation = 3;

Writer. WriteStartDocument ();

Releasing resources
Writer. Flush ();
Writer. Close ();
}

<summary>
Creates a new XML-formatted file (with the root node)
</summary>
<param name= "Rootnodename" > Root node name </param>
public void Createxml (string rootnodename)
{
Create a temporary file with the same name to ensure that the folder exists
Fobject.create (this. FilePath, fsomethod.file);

Create a XmlTextWriter instance
XmlTextWriter writer = new XmlTextWriter (this. FilePath, encoding.getencoding ("gb2312"));
Writer. formatting = formatting.indented;
Writer. indentation = 3;

Writer. WriteStartDocument ();
Start writing the root knot.
Writer. WriteStartElement (Rootnodename);
End Write root node
Writer. WriteEndElement ();

Writer. WriteEndDocument ();
Releasing resources
Writer. Flush ();
Writer. Close ();
}
#endregion


# region "Add"


<summary>
Creates a child node under the specified node and assigns a property or child node to the child node
</summary>
<param name= "NodeName" > Name of the child node to be created </param>
<param name= "DT" >
Storing assignment information for child nodes (a DataTable)
Row 1 (name, before the name contains the @ character, then the property)
Line 2 (value) </param>
public void CreateNode (string nodename, DataTable DT)
{
XmlDocument doc = new XmlDocument ();
Doc. Load (this. filePath);
XmlNode root = Doc. selectSingleNode (this. xPath);

Creating child nodes
XmlElement XE = Doc. CreateElement (nodename);
XmlElement xechild = null;
if (! Object.Equals (DT, NULL))
{
for (int i = 0; i < dt. Columns.count; i + +)
{
if (dt. Columns[i]. Columnname.startswith ("@"))
{
String attributename = dt. Columns[i]. Columnname.replace ("@", "");
Set properties for this child node
Xe. SetAttribute (attributename, dt. rows[0][i]. ToString ());
}
Else
{
Xechild = doc. createelement (dt. Columns[i]. ColumnName);
Try
{
Xechild.innerxml = dt. rows[0</

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.