XML Common Operations

Source: Internet
Author: User

 

Using system;
Using system. IO;
Using system. Data;
Using system. xml;
Using system. xml. XPath;

Namespace executexml
...{
Public class xmlcontrol
...{
Protected string strxmlfile;
Protected xmldocument objxmldoc = new xmldocument ();

Public xmlcontrol (string xmlfile)
...{
Try
...{
Objxmldoc. Load (xmlfile );
}
Catch (system. Exception ex)
...{
Throw ex;
}
Strxmlfile = xmlfile;
}

Public dataview getdata (string xmlpathnode)
...{
Dataset DS = new dataset ();
Stringreader READ = new stringreader (objxmldoc. selectsinglenode (xmlpathnode). outerxml );
DS. readxml (read );
Return Ds. Tables [0]. defaultview;
}

Public void Replace (string xmlpathnode, string content)
...{
Objxmldoc. selectsinglenode (xmlpathnode). innertext = content;
}

Public void Delete (string node)
...{
String mainnode = node. substring (0, node. lastindexof ("/"));
Objxmldoc. selectsinglenode (mainnode). removechild (objxmldoc. selectsinglenode (node ));
}

Public void insertnode (string mainnode, string childnode, string element, string content)
...{
Xmlnode objrootnode = objxmldoc. selectsinglenode (mainnode );
Xmlelement objchildnode = objxmldoc. createelement (childnode );
Objrootnode. appendchild (objchildnode );
Xmlelement objelement = objxmldoc. createelement (element );
Objelement. innertext = content;
Objchildnode. appendchild (objelement );
}

Public void insertelement (string mainnode, string element, string attrib, string attribcontent, string content)
...{
Xmlnode objnode = objxmldoc. selectsinglenode (mainnode );
Xmlelement objelement = objxmldoc. createelement (element );
Objelement. setattribute (attrib, attribcontent );
Objelement. innertext = content;
Objnode. appendchild (objelement );
}

Public void insertelement (string mainnode, string element, string content)
...{
Xmlnode objnode = objxmldoc. selectsinglenode (mainnode );
Xmlelement objelement = objxmldoc. createelement (element );
Objelement. innertext = content;
Objnode. appendchild (objelement );
}

Public void save ()
...{
Try
...{
Objxmldoc. Save (strxmlfile );
}
Catch (system. Exception ex)
...{
Throw ex;
}
Objxmldoc = NULL;
}
}
}

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.