Public class xmlcontrol {protected string strXmlfile; protected xmldocument objxmldoc = new xmldocument ();
Public XMLControl (String XMLFile) {// // TODO: Add to construct a program code // 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 content. Objxmldoc.selectsinglenode (xmlpathnode) .INNNERTEXT = Content;}
Public void delete (String node) {// Deletes a node. String mainnode = node.substring (0, node.lastindexof ("/")); objxmldoc.selectsinglenode (mainnode) .removechild (Objxmldoc.selectsinglenode);}
Public void insertNode (String Mainnode, String ChildNode, String Element, String Content) {// Insert a node and one sub-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 attrib, string content) {// Insert 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);}
Public void insertelement (String Mainnode, String Element, String Content) {// Insert a node, without attributes. XmlNode objNode = objXmlDoc.SelectSingleNode (MainNode); XmlElement objElement = objXmlDoc.CreateElement (Element); objElement.InnerText = Content; objNode.AppendChild (objElement);} public void Save () {// save the document. Try {objxmldoc.save (strxmlfile);} catch (system.exception ex) {throw ex;} Objxmldoc = null;}} ====================== ================================================================================================== # XMLControl XmlTool = New XMLControl (strXmlfile);
// Data Note // DGList.DataSource = Xmltool.getData ("Book / Authors [ISBN = /" 0002 / "]"); // dglist.database ();
// Update element content // xmltool.replace ("Book / Authors [ISBN = /" 0002 / "] / content", "pppppp"); // xmltool.save ();
// Add a new node // xmltool.insertnode ("Book", "Author", "ISBN", "0004"); // XmlTool.InsertElement ("Book / Author [ISBN = /"] ", "Content", "Aaaaaaaaaa"); // Xmltool.insertelement ("Book / Author [ISBN = /" 0004 / "]", "limited", "man", "iiiiiiiii"); // xmltool .Save ();
/ / Delete all content and attributes of a specified node // xmltool.delete ("Book / Author [ISBN = /" 0004 / "]"); // xmltool.save ();
/ / Delete the child node of a specified node // xmltool.delete ("Book / Authors [ISBN = /" 0003 / "]"); // xmltool.save ();