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

xiaoxiao2021-03-05  28

Public Class Cxml

{

Private string strXmlfile;

Private xmldocument objxmldoc = new xmldocument ();

Public Cxml (String XMLFile)

{

//Constructor

Try

{

Objxmldoc.Load (XMLFile);

}

Catch

{

}

Strxmlfile = Xmlfile;

}

Public DataView getData (String Xmlpathnode)

{

// Find data to return 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) .INNNERTEXT = Content;

}

Public void delete (String Node)

{

/ / Delete a node

String mainnode = node.substring (0, node.lastIndexof ("/"));

Objxmldoc.selectsinglenode (Mainnode) .removechild (Objxmldoc.selectsinglenode);

}

Public void insertnode (String MAINNODE, STRING Element, String Content)

{

// Insert a node and a child node of this node

XMLNode Objrootnode = Objxmldoc.selectsinglenode (Mainnode);

Xmlelement ObjchildNode = Objxmldoc.createElement (childnode);

Objrootnode.Appendchild (ObjchildNode);

Xmlelement Objlement = Objxmldoc.createElement (Element);

Objlement.innertext = Content;

ObjchildNode.Appendchild (Objlement);

}

Public void insertelement (String MainNode, String Element, string Attrib, String Attribcontent, String Content)

{

// Insert a node with an attribute

XMLNode ObjNode = Objxmldoc.selectsinglenode (MainNode);

Xmlelement Objlement = Objxmldoc.createElement (Element);

Objlement.setttribute (attrib, attrib,);

Objlement.innertext = Content;

ObjNode.AppendChild (Objlement);

}

Public void insertelement (String MainNode, String Element, String Content)

{

// Insert a node without attribute

XMLNode ObjNode = Objxmldoc.selectsinglenode (Mainnode); Xmlelement Objlement = Objxmldoc.createElement (Element);

Objlement.innertext = Content;

ObjNode.AppendChild (Objlement);

}

Public void save ()

{

/ / Save an XML file

Try

{

Objxmldoc.save (strxmlfile);

}

Catch

{

}

Objxmldoc = NULL;

}

}

转载请注明原文地址:https://www.9cbs.com/read-37668.html

New Post(0)