A more useful XML file operation Class C # code can continue to expand

Source: Internet
Author: User
Tags continue insert
xml| comparison

public class CXml

{

private string Strxmlfile;

Private XmlDocument objxmldoc = new XmlDocument ();

Public CXml (String xmlFile)

{

Constructors

Try

{

Objxmldoc.load (XmlFile);

}

Catch

{

}

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 content

Objxmldoc.selectsinglenode (Xmlpathnode). innertext = content;

}

public void Delete (String node)

{

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

{

Insert a node with one attribute

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)

{

Insert 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 XML file

Try

{

Objxmldoc.save (Strxmlfile);

}

Catch

{

}

objXMLDoc = null;

}

}

Related Article

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.