XML Operations Class

Source: Internet
Author: User

public class Xmlcontrol

{

protected string Strxmlfile;

protected XmlDocument objxmldoc = new XmlDocument ();

Public Xmlcontrol (String XmlFile)

{

//

TODO: Add the constructor program code here

//

Try

{

Objxmldoc.load (XmlFile);

}

catch (System.Exception ex)

{

Throw ex;

}

Strxmlfile = XmlFile;

}

Public DataView GetData (string xmlpathnode)

{

Find data. Returns a DataView

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)

{

Update node contents.

Objxmldoc.selectsinglenode (Xmlpathnode). innertext = Content;

}

public void Delete (string Node)

{

Deletes a 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)

{

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);

}

public void Insertelement (string mainnode,string element,string attrib,string attribcontent,string Content)

{

Inserts a node with a property.

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)

{

Inserts a node with no attributes.

XmlNode objnode = Objxmldoc.selectsinglenode (Mainnode);

XmlElement objelement = objxmldoc.createelement (Element);

Objelement.innertext = Content;

Objnode.appendchild (objelement);

}

public void Save ()

{

Save the document file.

Try

{

Objxmldoc.save (Strxmlfile);

}

catch (System.Exception ex)

{

Throw ex;

}

objXMLDoc = null;

}

}

=========================================================

Instance 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 attributes for 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 ();

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.