C # Common Operations Class Library III (XML operation Class)

Source: Internet
Author: User

/// <summary>///a summary description of the xmlhelper. ///XML Operation Classes/// </summary> Public classxmlhelper{protected stringStrxmlfile; protectedXmlDocument objXMLDoc =NewXmlDocument (); PublicXmlhelper (stringxmlfile) {//     //TODO: Add the program code for the constructor here//     Try{objxmldoc.load (xmlfile); }Catch(System.Exception ex) {Throwex; } strxmlfile=xmlfile;} PublicDataTable GetData (stringXmlpathnode) {//find data. Returns a DataViewDataSet ds =NewDataSet (); StringReader Read=NewStringReader (Objxmldoc.selectsinglenode (Xmlpathnode).  OuterXml); Ds.  READXML (read); returnDs. tables[0];}/// <summary>///The content of the new node. ///Example: Xmltool.replace ("book/authors[isbn=\" 0002\ "]/content", "PPPPPPP");/// </summary>/// <param name= "Xmlpathnode" ></param>/// <param name= "Content" ></param> Public voidReplace (stringXmlpathnode,stringContent) {//updates the node content. Objxmldoc.selectsinglenode (Xmlpathnode). InnerText =Content;}/// <summary>///deletes a child node of a specified node. ///Example: Xmltool.deletechild ("book/authors[isbn=\" 0003\ "]");/// </summary>/// <param name= "Node" ></param> Public voidDeletechild (stringNode) {//deletes a node.     stringMainnode = node.substring (0, Node.lastindexof ("/")); Objxmldoc.selectsinglenode (Mainnode). RemoveChild (Objxmldoc.selectsinglenode (Node));} /// <summary>///* Use the following columns:///Example: Xmlhelper.delete ("/node", "" ")///xmlhelper.delete ("/node", "Attribute")/// </summary>/// <param name= "Node" >node</param>/// <param Name= "attribute" >property name, not empty when the node property value is deleted, otherwise the node value is deleted</param> Public voidDelete (stringNodestringattribute) {Try{XmlNode xn=objxmldoc.selectsinglenode (node); XmlElement XE=(XmlElement) xn; if(attribute. Equals ("") ) xn.    Parentnode.removechild (xn); ElseXE.  RemoveAttribute (attribute); }Catch { }}/// <summary>///inserts a node and a child node of this node. ///Example: Xmltool.insertnode ("book", "Author", "ISBN", "0004");/// </summary>/// <param name= "Mainnode" >Master Node</param>/// <param name= "Childnode" >child Nodes</param>/// <param name= "Element" >Elements</param>/// <param name= "Content" >content</param> Public voidInsertnode (stringMainnode,stringChildnode,stringElement,stringContent) {//inserts a node and a child node of this node. XmlNode Objrootnode =Objxmldoc.selectsinglenode (Mainnode); XmlElement Objchildnode=objxmldoc.createelement (Childnode); Objrootnode.appendchild (Objchildnode); XmlElement objelement=objxmldoc.createelement (Element); Objelement.innertext=Content;objchildnode.appendchild (objelement);}/// <summary>///inserts a node with an attribute. ///Example: Xmltool.insertelement ("book/author[isbn=\" 0004\ "]", "Title", "Sex", "Man", "IIIIIIII");/// </summary>/// <param name= "Mainnode" >Master Node</param>/// <param name= "Element" >Elements</param>/// <param name= "Attrib" >Properties</param>/// <param name= "Attribcontent" >Property Content</param>/// <param name= "Content" >element Content</param> Public voidInsertelement (stringMainnode,stringElement,stringAttrib,stringAttribcontent,stringContent) {//inserts a node with an attribute. XmlNode Objnode =Objxmldoc.selectsinglenode (Mainnode); XmlElement objelement=objxmldoc.createelement (Element); Objelement.setattribute (Attrib, attribcontent); Objelement.innertext=Content;objnode.appendchild (objelement);}/// <summary>///inserts a node without attributes. ///Example: Xmltool.insertelement ("book/author[isbn=\" 0004\ "]", "Content", "aaaaaaaaa");/// </summary>/// <param name= "Mainnode" >Master Node</param>/// <param name= "Element" >Elements</param>/// <param name= "Content" >element Content</param> Public voidInsertelement (stringMainnode,stringElement,stringContent) {//inserts a node without attributes. XmlNode Objnode =Objxmldoc.selectsinglenode (Mainnode); XmlElement objelement=objxmldoc.createelement (Element); Objelement.innertext=Content;objnode.appendchild (objelement);}/// <summary>///INSERT, UPDATE, delete the XML file and save the changes/// </summary> Public voidSave () {//Save your files. Try{objxmldoc.save (strxmlfile);}Catch(System.Exception ex) {Throwex;} objXMLDoc=NULL;}}//=========================================================//Example Application://string strxmlfile = Server.MapPath ("Testxml.xml");//Xmlcontrol xmltool = new Xmlcontrol (strxmlfile);//Data Display//Dglist.datasource = Xmltool.getdata ("book/authors[isbn=\" 0002\ "]");//Dglist.databind ();//Update element content//xmltool.replace ("book/authors[isbn=\" 0002\ "]/content", "PPPPPPP");//Xmltool.save ();//Add a new node//Xmltool.insertnode ("book", "Author", "ISBN", "0004");//xmltool.insertelement ("book/author[isbn=\" 0004\ "]", "Content", "aaaaaaaaa");//xmltool.insertelement ("book/author[isbn=\" 0004\ "]", "Title", "Sex", "Man", "IIIIIIII");//Xmltool.save ();//deletes all content and properties of a specified node//xmltool.delete ("book/author[isbn=\" 0004\ "]");//Xmltool.save ();//deletes a child node of a specified node//xmltool.delete ("book/authors[isbn=\" 0003\ "]");//Xmltool.save ();

C # Common Operations Class Library III (XML operation Class)

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.