Use XML in C # to implement DOM

xiaoxiao2021-03-06  41

In the first two articles, we discussed the reading and writing of XML files, but all based on stream model solutions. Today we will talk about how to achieve DOM in C #, DOM does have it, but It is still an indispensable technology in programming work. Let's take a brief understanding of the relevant knowledge of the DOM.

The full name of the DOM is Document Object Model (Document Object Model), it is from W

3C

Official standards, it allows W

3C

Standard W

3C

Dom level1 and w

3C

The rules defined by the Dom Level2 specification are read, manipulated, and modified XML documents. The DOM works: first load the XML document one-time load into memory, then create a "tree structure" in memory according to the elements and attributes defined in the document, which is a document object model. The meaning here is actually putting a document. Objectualization, each node in the document corresponds to an object in the model, and we all know that the object provides programming interface, so we are using this group of objects to access the XML document to operate the XML document, and the following picture shows Application and DOM interaction process:

Since DOM creates a tree structure view in memory to provide a programming interface, then we will show how DOM creates a tree structure below:

> Text Here

If you load the above document with a DOM, it will be found in the memory as shown below:

The key to the DOM is that it allows direct update of the tree structure in the memory, without having to reach other output, therefore, adding, updating, or deleting the operation efficiency of information in the structure. As a programmer, we are important to understand the programming interface provided by the DOM to implement the XML document, in fact, .NET Framework defines a set of architectures to reflect DOM, let's take a look. Net The inheritance structure of the DOM:

The classes included in all arc angular rectangles in the figure describe all node types that may appear in the XML document, and the operation XML document is nothing more than the node of the operation, and these classes are derived from the XMLNode class. So our theme today is to discuss the XMLNode class and its subclass XMLDocument, which is a simple introduction to these classes:

XMLNode class:

This class is an abstract base class of all other nodes in the DOM defines all members inherited or rewritten in the lower class class. It represents a single node in the XML document that provides basic methods and properties for navigation DOM tree structure, using the XMLNodeType enumerator to enumerate all node types under this. The partial properties and methods of this class are described below:

Attributes:

[C #]

Public Virtual Bool HaschildNodes {get;} Get a value indicating whether the current node has any child nodes

Public Virtual XMLNodelist ChildNodes {get;} Get all child nodes of the current node

Public Virtual XMLNode Firstchild {Get;} Get the first child of the current node

Public Virtual XMLNode LastChild {get;} Get the last child of the current node

Public Virtual XMLNode ParentNode {get;} Get the parent public virtual xmlnode nextsibling {get;} for the current node to get the next brothers node

Public Virtual XMLNode Previoussibling {get;} Get the previous brothers node of the current node

Public Virtual String Innertext {get; set;} Get or set the series value of the current node and all the subtots of all the child nodes

Public Virtual String InnerXml {get; set;} Get or set tags that represent only the child node representing the current node

Public Virtual String OuterXML {get;} Get tags indicating the current node and all of the child nodes

method:

Public XMLNodelist SelectNodes (String); Select a list of nodes that match XPath expressions in the document

Public XMLNode Selectsinglenode (String); Select the first XMLNode that matches the XPath expression in the document

Public Virtual XMLNode appendchild (XMLNode Newchild) Adds the specified node to the end of the child node list of the node

Public Virtual XMLNode PrependChild (XMLNode Newchild) Adds the specified node to the beginning of the child's child list list

Public Virtual XMLNode RemoveChild (XMLNode Oldchild) Removes the specified child node

Public Virtual XMLNode Replacechild (XMLNode Newchild, XMLnode Oldchil replaces child nodes with newchild nodes

XMLNodeList class:

This class represents an ordered collection of XMLNode, which has the following common members:

Count - Returns the number of nodes in XMLNodelist in an integer

ItemOF - Search Nodes at the specified index

GetEnumerator () - provides the foreach style of the iterative traversal node list

Item () - Returns the node at the index specified by the parameter

XMLDocument class:

The XMLDocument class is the .NET representation of the XML document, which represents the document node of the tree structure in the memory (all nodes are in the document node), and the XMLDocument class contains all Createxxxx () methods, which allows all derived self The node of the type of XMLNode typically uses the class with the XMLNode class to complete the operation of the document, which has a LOAD () method for loading an XML document, which allows one overload version of this method to load documents from XMLTextReader, this The benefits of bringing us is that when we operate large documents, we can use XMLTextReader to filter the unrelated document section, which solves the resource loss problem brought by the DOM, which can keep DOM's convenience of document control. The class save () method is used to save the document. Next, use a simple example to explain how to implement the DOM in C #, look at the running renderings before watching the code:

LoadXML button is used to load an XML document, LoadXMLReader button to use the XmlTextReader to load the document, SaveXML button to save the document, SaveXMLWriter button to save the document to the XmlTextWriter, Add Product button to add nodes, Replace Product button replacement node, Change Order by New modification document, Remove Product Info press New Remove Node.

The Domoperation class encapsulates all the implementations of the New button, the code is as follows:

Namespace Domsamples

{

Using system;

USING SYSTEM.XML;

Using system.text;

Using system.windows.forms;

Using system.componentmodel;

///

/// Domopertroom provides classes for XML file operations

///

///

/// This class is used to provide general operations for XML files, such as (add, delete, replace nodes, load, saving files), etc., the internal implementation of DOM technology.

///

Public Class Domopertroom: IDisposable

{

Private string _xmlpath;

Private xmldocument Xmldoc;

#REGON DOMOPERATION constructor

///

/// Default constructor, default assignment to this class

///

Public Domoperation ()

{

THIS._XMLPATH = String.empty;

THIS.XMLDOC = NULL;

}

///

/// Take a refined constructor, initially assignment of such members

///

/// XML file path

Public Domoperation (String XMLPATH)

{

THIS._XMLPATH = XMLPATH;

THIS.XMLDOC = NULL;

}

#ndregion

#Region Domoperation resource release method

///

/// Clean all the resources being used by this object

/// public void dispose ()

{

THIS.DISPOSE (TRUE);

Gc.suppressFinalize (this);

}

///

/// Release the instance variable of the object

///

///

Protected Virtual Void Dispose (BOOL Disposing)

{

IF (! Disposing)

Return;

IF (this._xmlpath! = null)

{

THIS._XMLPATH = NULL;

}

IF (this.xmldoc! = null)

{

THIS.XMLDOC = NULL;

}

}

#ndregion

#Region Domoperation property

///

/// Get or set the path to the XML file

///

Public String XMLPath

{

get

{

Return_Xmlpath;

}

set

{

THIS._XMLPATH = VALUE;

}

}

#ndregion

///

// / Load XML file

///

///

// / This method uses the path to the XML file to load the XML file and return the content of the entire XML file.

///

/// The entire XML file content

Public string load ()

{

XMLDoc = new xmldocument ();

Try

{

XMLDoc.Load (this._xmlpath);

}

Catch (XMLException XMLEXP)

{

Throw new xmlexception (xmlexp.tostring ());

}

Return XMLDoc.outerxml;

}

///

// / Load XML file

///

///

/// This method uses XMLReader to locate and load the XML file in the XML document, and then return to the content of the XML file.

///

/// Node type of node to be positioned

/// Node name to be positioned

/// XML file content

Public String LoadByxmlReader (XMLNodType Nodetype, String Localname)

{

String xmlstr = string.empty;

XMLTextReader XmltXtrd = New XMLTextReader (this._xmlpath);

XMLDoc = new xmldocument ();

Try

{

// Locate in the document

While (Xmltxtrd.Read ())

{

IF (xmltxtrd.nodetype == nodetype)

IF (xmltxtrd.localname == localname)

Break;

}

XMLDoc.Load (XMLTXTRD);

Xmltxtrd.close ();

XMLSTR = "===== OuterXML =====";

XMLSTR = system.environment.newline;

XMLSTR = Xmldoc.firstchild.outerXML;

XMLSTR = system.environment.newline;

XMLSTR = system.environment.newline;

XMLSTR = "===== innerXml =====";

XMLSTR = system.environment.newline;

XMLSTR = Xmldoc.firstchild.innerxml;

XMLSTR = system.environment.newline;

XMLSTR = system.environment.newline;

XMLSTR = "===== innertext =====";

XMLSTR = system.environment.newline;

XMLSTR = Xmldoc.firstchild.innertext;

XMLSTR = system.environment.newline;

XMLSTR = system.environment.newline;

XMLSTR = "===== value =====";

XMLSTR = system.environment.newline;

XMLSTR = Xmldoc.firstchild.childNodes [0] .childNodes [0] .value ""

Xmldoc.FirstChild.childNodes [1] .childnodes [0] .value ""

Xmldoc.FirstChild.childNodes [2] .childnodes [0] .value;

XMLSTR = system.environment.newline;

XMLSTR = system.environment.newline;

}

Catch (XMLException XMLEXP)

{

Throw new xmlexception (xmlexp.tostring ());

}

Finally

{

IF (xmltxtrd! = null && xmltXtrd.readstate! = readstate.closed)

Xmltxtrd.close ();

}

Return XMLSTR;

}

///

// / Save an XML file

///

///

// / This method saves the incoming XML text in the incoming path to finally return the content of the XML file

///

/// XML text

/// Save Path /// XML file

Public String Save (String XmlText, String SavePath)

{

XMLDoc = new xmldocument ();

Try

{

XMLDoc.loadxml (XMLText);

Xmldoc.save (SavePath);

XMLDoc.Load (SavePath);

}

Catch (XMLException XMLEXP)

{

Throw new xmlexception (xmlexp.tostring ());

}

Return XMLDoc.outerxml;

}

///

// / Save an XML file

///

///

// / This method saves the incoming XML text in the XMLTextWriter writer and constructs the writer last returns the content of the XML file using the incoming path.

///

/// XML text

/// Save Path

/// XML file content

Public String SavebyXMLWRITER (String XmlText, String SavePath)

{

XmlTextWriter XMLTXTWT = New XmlTextWriter (SavePath, Encoding.Unicode);

XMLDoc = new xmldocument ();

Try

{

XMLDoc.loadxml (XMLText);

XMLDoc.save (XMLTXTWT);

Xmltxtwt.close ();

XMLDoc.Load (SavePath);

}

Catch (XMLException XMLEXP)

{

Throw new xmlexception (xmlexp.tostring ());

}

Finally

{

IF (xmltxt wt! = null && xmltxtwt.writestate! = WriteState.Closed)

Xmltxtwt.close ();

}

Return XMLDoc.outerxml;

}

///

/// Add a child node

///

///

// / This method uses the incoming parent node path to select the node and add the incoming XML document clip to the parent node finally returns the content of the XML file.

///

///

/// XML document clip

/// Parent Node Path

/// Add after the XML file

Public String AddChildNode (XmLDocument Xmldoc, XmLDocumentFragment XmLDocfrag, String ParentnodePath)

{

THIS.XMLDOC = XMLDoc; Xmlelement SelectEle = (XMLELEMENT) XMLDoc.selectsingLenode (ParentnodePath);

SelectEle.Appendchild (Xmldocfrag.firstchild);

Return this.xmldoc.outerxml;

}

///

/// Replace the child node

///

///

// / This method uses the parent node path to select the node and replace the contents of the first sub-node under the parent node in the parent node with the incoming XML document segment.

///

///

/// XML document clip

/// Parent Node Path

/// 第 个 节

/// The content of the XML file after replacing

Public String ReplaceChildNode (XmlDocument Xmldoc, XmldocumentFragment Xmldocfrag, String ParentnodePath, Int i)

{

THIS.XMLDOC = XMLDOC;

XMLELEMENT SECTELE = (XMLEMENT) XMLDoc.selectsinglenode (ParentNodePath);

Xmlelement Childele = (XMLELEMENT) Selectle.childNodes [i];

Selectele.replacechild (xmldocfrag.firstchild, childle);

Return this.xmldoc.outerxml;

}

///

/// Remove child node

///

///

// / This method uses the incoming parent node name to select the parent node and use the incoming sub-node name to select the sub-node RemoveChild method to remove the sub-node.

/// finally returns the file content of the XML after the removal

///

/// Parent Node Name

/// child node name

/// Remove XML file content

Public String RemoveChildNode (String ChildNodename)

{

XMLDoc = new xmldocument ();

XMLDoc.Load (this._xmlpath);

XMLnodelist ParentnodeList = XmLDoc.GetElementsBytagname (ParentNodename);

Foreach (XMLNode Parentnode in ParentnodeList)

{

XMLnodelist ChildNodelist = ParentNode.selectNodes (ChildNodename); Foreach (XMLNode ChildNode In ChildNodeList)

{

ParentNode.RemoveChild (childnode);

}

}

Return XMLDoc.outerxml;

}

}

}

The window program code is as follows:

Namespace Domsamples

{

Using system;

USING SYSTEM.XML;

Using system.text;

Using system.drawing;

Using system.collections;

Using system.componentmodel;

Using system.windows.forms;

Using system.data;

///

/// Form1 summary description.

///

Public Class Form1: System.Windows.Forms.form

{

Private system.windows.Forms.TextBox textBox1;

Private system.windows.Forms.Button button1;

Private system.windows.Forms.Button Button2;

Private system.windows.Forms.Button Button3;

Private system.windows.Forms.Button Button4;

Private system.windows.Forms.Button Button5;

Private system.windows.Forms.Button button;

Private system.windows.Forms.Button Button7;

Private system.windows.Forms.Button button;

Private string xmlpath;

///

/// The required designer variable.

///

Private system.componentmodel.Container Components = NULL;

Public Form1 ()

{

//

// Windows Form Designer Support

//

InitializationComponent ();

//

// Todo: Add any constructor code after INITIALIZECOMPONENT call

//

}

///

/// Clean all the resources being used.

///

Protected Override Void Dispose (Bool Disposing)

{

IF (Disposing)

{

IF (Components! = NULL)

{

Components.dispose ();

}

}

Base.dispose (Disposing);

}

#Region Windows Form Designer Generated Code

///

/// Designer supports the required method - do not use the code editor to modify

/// This method is content.

///

Private vidinitiRizeComponent ()

{

This.TextBox1 = new system.windows.Forms.TextBox ();

This.button1 = new system.windows.Forms.Button ();

This.button2 = new system.windows.Forms.Button (); this.button3 = new system.windows.Forms.Button ();

This.button4 = new system.windows.Forms.Button ();

This.button5 = new system.windows.Forms.Button ();

This.button6 = new system.windows.forms.button ();

This.button7 = new system.windows.forms.button ();

THIS.BUTTON8 = new system.windows.forms.button ();

THIS.SUSPENDLAYOUT ();

//

// textbox1

//

This.TextBox1.anchor = ((System.Windows.Forms.Anchorstyles) (((((System.Windows.Forms.Anchorstyles.top | System.Windows.Forms.Anchorstyles.Bottom)

| System.windows.Forms.Anchorstyles.Left)

| System.windows.Forms.Anchorstyles.right)))))))))))))))))))))))

This.TextBox1.Location = new system.drawing.point (8, 8);

THIS.TEXTBOX1.MULTILINE = TRUE;

THIS.TEXTBOX1.NAME = "TextBox1";

THIS.TEXTBOX1.Scrollbars = system.windows.forms.scrollbars.both;

THIS.TEXTBOX1.SIZE = New System.drawing.size (776, 296);

this.TextBox1.tabindex = 0;

THIS.TEXTBOX1.TEXT = "";

//

// Button1

//

This.Button1.anchor = ((System.Windows.Forms.Anchorstyles) ((System.Windows.Forms.Anchorstyles.Bottom | System.Windows.Forms.Anchorstyles.left))));

This.button1.location = new system.drawing.point (8, 312);

This.button1.name = "button1";

This.button1.size = new system.drawing.size (56, 23);

This.button1.tabindex = 1;

this.button1.text = "loadingXML";

This.Button1.click = new system.eventhandler (this.button1_click);

//

// Button2

//

This.Button2.Anchor = ((System.Windows.Forms.Anchorstyles) ((System.Windows.Forms.Anchorstyles.Bottom | System.Windows.Forms.Anchorstyles.left));

This.Button2.Location = new system.drawing.point (72, 312);

This.button2.name = "button2"; this.button2.size = new system.drawing.size (96, 23);

This.button2.tabindex = 2;

this.button2.text = "loadingxmlreader";

This.button2.click = new system.eventhandler (this.button2_click);

//

// Button3

//

This.button3.anchor = ((System.Windows.Forms.Anchorstyles) ((System.Windows.Forms.Anchorstyles.Bottom | System.Windows.Forms.Anchorstyles.left));

This.Button3.Location = new system.drawing.point (176, 312);

This.button3.name = "button3";

This.button3.size = new system.drawing.size (56, 23);

this.button3.tabindex = 3;

This.Button3.Text = "savexml";

This.Button3.click = new system.eventhandler (this.button3_click);

//

// Button4

//

This.Button4.Anchor = ((System.Windows.Forms.Anchorstyles) ((System.Windows.Forms.Anchorstyles.Bottom | System.Windows.Forms.Anchorstyles.left));

This.Button4.Location = new system.drawing.point (240, 312);

This.button4.name = "button4";

This.button4.size = new system.drawing.size (96, 23);

This.button4.tabindex = 4;

This.button4.text = "savexmlwriter";

This.button4.click = new system.eventhandler (this.button4_click);

//

// Button5

//

This.Button5.Anchor = ((System.Windows.Forms.Anchorstyles) ((System.Windows.Forms.Anchorstyles.Bottom | System.Windows.Forms.Anchorstyles.left));

This.button5.location = new system.drawing.point (344, 312);

This.button5.name = "button5";

This.button5.size = new system.drawing.size (80, 23);

This.button5.tabindex = 5;

this.button5.text = "add product";

This.button5.click = new system.eventhandler (this.button5_click);

//

// Button6 //

This.Button6.Anchor = ((System.Windows.Forms.Anchorstyles) ((System.Windows.Forms.Anchorstyles.Bottom | System.Windows.Forms.Anchorstyles.left));

This.Button6.Location = new system.drawing.point (432, 312);

This.button6.name = "button6";

This.button6.size = new system.drawing.size (112, 23);

This.button6.tabindex = 6;

This.button6.text = "replace products";

This.button6.click = new system.eventhandler (this.button6_click);

//

// Button7

//

This.Button7.anchor = ((System.Windows.Forms.Anchorstyles) ((System.Windows.Forms.Anchorstyles.Bottom | System.Windows.Forms.Anchorstyles.left));

This.Button7.Location = new system.drawing.point (552, 312);

this.button7.name = "button7";

This.button7.size = new system.drawing.size (88, 23);

THIS.BUTTON7.TABINDEX = 7;

This.button7.text = "change order";

This.button7.click = new system.eventhandler (this.button7_click);

//

// Button8

//

This.Button8.Anchor = ((System.Windows.Forms.Anchorstyles) ((System.Windows.Forms.Anchorstyles.Bottom | System.Windows.Forms.Anchorstyles.left)));

This.button8.location = new system.drawing.point (648, 312);

This.button8.name = "button8";

This.button8.size = new system.drawing.size (136, 23);

This.button8.tabindex = 8;

This.button8.text = "remove product info";

This.Button8.click = new system.eventhandler (this.button8_click);

//

// Form1

//

THIS.AUTOSCALEBASESIZE = New System.drawing.size (6, 14);

THIS.CLIENTSIZE = New System.drawing.size (792, 341);

This.Controls.add (this.button8);

This.Controls.add (this.button7);

This.Controls.add (this.button6);

This.Controls.Add (this.button5); this.controls.add (this.button4);

This.Controls.add (this.button3);

This.Controls.add (this.button2);

This.Controls.add (this.button1);

This.Controls.add (this.TextBox1);

THIS.NAME = "Form1";

THIS.TEXT = "Domsample Application";

This.ResumeLayout (false);

//

// xmlpath

//

THIS.XMLPATH = "../../sample.xml";

}

#ndregion

///

/// The main entry point for the application.

///

[Stathread]

Static void

Main

()

{

Application.run (New Form1 ());

}

Private void Button1_Click (Object Sender, System.Eventargs E)

{

THIS.TEXTBOX1.TEXT = "";

Domopression Domoper = New Domoperation (this.xmlpath);

Try

{

This.TextBox1.text = domoper.load ();

}

Catch (XMLException XMLEXP)

{

Messagebox.show (XmLexp.toString (), "Error", MessageBoxButtons.ok, MessageBoxicon.Error);

}

Catch (Exception Exp)

{

Messagebox.show (Exp.toT7tring (), "Error", MessageBoxButtons.ok, MessageBoxicon.Error);

}

}

Private void button2_click (Object Sender, System.Eventargs E)

{

THIS.TEXTBOX1.TEXT = "";

Domopression Domoper = New Domoperation (this.xmlpath);

Try

{

THIS.TEXTBOX1.TEXT = Domoper.loadByxmlReader (XMLNodettype.element, "Product");

}

Catch (XMLException XMLEXP)

{

Messagebox.show (XmLexp.toString (), "Error", MessageBoxButtons.ok, MessageBoxicon.Error);

}

Catch (Exception Exp)

{

Messagebox.show (Exp.toT7tring (), "Error", MessageBoxButtons.ok, MessageBoxicon.Error);

}

}

Private void button3_click (Object Sender, System.Eventargs E)

{

String savePath = "../../rootdoc.xml";

String Xmltext = " Some Para Text ";

Domopression Domoper = New Domoperation (); TRY

{

This.TextBox1.text = Domoper.save (XMLText, SavePath);

}

Catch (XMLException XMLEXP)

{

Messagebox.show (XmLexp.toString (), "Error", MessageBoxButtons.ok, MessageBoxicon.Error);

}

Catch (Exception Exp)

{

Messagebox.show (Exp.toT7tring (), "Error", MessageBoxButtons.ok, MessageBoxicon.Error);

}

}

Private void button4_click (Object Sender, System.Eventargs E)

{

String savepath = "../../rootdoc2.xml";

String XmlText = " Some Para Text

Domopression Domoper = New Domoperation ();

Try

{

This.TextBox1.text = domoper.savebyxmlwriter (XmlText, SavePath);

}

Catch (XMLException XMLEXP)

{

Messagebox.show (XmLexp.toString (), "Error", MessageBoxButtons.ok, MessageBoxicon.Error);

}

Catch (Exception Exp)

{

Messagebox.show (Exp.toT7tring (), "Error", MessageBoxButtons.ok, MessageBoxicon.Error);

}

}

Private void button5_click (Object Sender, System.Eventargs E)

{

Domopression Domoper = New Domoperation ();

XMLDocument Xmldoc = new xmldocument ();

Try

{

XMLDoc.Load (this.xmlpath);

// Create an XML document clip

XMLDocumentFragment Xmldocfrag = xmldoc.createdocumentfragment ();

// Create a parent root element node test: Product

XmLelement Prole = Xmldoc.createElement ("Test", "Product", "URI: Test");

// Create an attribute and add it to the root element TEST: Product

XMlattribute ProIDatt = XmLDoc.createAttribute ("ProductID");

ProIDatt.value = "MU78";

Prole.attributes.setnamedItem (ProIDatt);

// Creating a leaf node of the root element node

Xmlelement Colorele = Xmldoc.createElement ("Color");

Colorele.innertext = "red";

Prole.Appendchild (Colorele); XmLelement Sizeele = XmLDoc.createElement ("size"); "SIZE");

Sizeele.innertext = "m";

Prole.Appendchild (Sizeele);

XmLelement PriceEle = XmLDoc.createElement ("Price");

PriceLe.innertext = "125.99";

Prole.Appendchild (PriceEle);

// Add the root element into the XML document segment

Xmldocfrag.Appendchild (Prole);

THIS.TEXTBOX1.TEXT = Domoper.AddchildNode (XMLDoc, XmLDocfrag, "// ProductFamily");

}

Catch (Exception Exp)

{

Messagebox.show (Exp.toT7tring (), "Error", MessageBoxButtons.ok, MessageBoxicon.Error);

}

}

Private void button6_click (Object Sender, System.Eventargs E)

{

XMLDocument Xmldoc = new xmldocument ();

Domopression Domoper = New Domoperation ();

Try

{

XMLDoc.Load ("../../ Sample.xml");

XMLDocumentFragment Xmldocfrag = xmldoc.createdocumentfragment ();

XmLelement Prole = Xmldoc.createElement ("Test", "Product", "URI: Test");

XMlattribute ProIDatt = XmLDoc.createAttribute ("ProductID");

ProIDatt.value = "MU78";

Prole.attributes.setnamedItem (ProIDatt);

Xmlelement Colorele = Xmldoc.createElement ("Color");

Colorele.innertext = "red";

Prole.AppendChild (Colorele);

XmLelement Sizeele = Xmldoc.createElement ("size");

Sizeele.innertext = "m";

Prole.Appendchild (Sizeele);

XmLelement PriceEle = XmLDoc.createElement ("Price");

PriceLe.innertext = "125.99";

Prole.Appendchild (PriceEle);

Xmldocfrag.Appendchild (Prole);

THIS.TEXTBOX1.TEXT = Domoper.ReplaceChildNode (Xmldoc, XmLDocfrag, "// ProductFamily", 0);

}

Catch (Exception Exp)

{

Messagebox.show (Exp.Tostring (), "Error", MessageBoxButtons.ok, MessageBoxicon.Error);

}

// Modify the XML document directly using InnerXML and Innertext

Private void button7_click (Object Sender, System.Eventargs E)

{

XMLDocument Xmldoc = new xmldocument ();

XMLDoc.Load ("../../ Sample.xml");

Xmlnodelist nodelist = xmldoc.selectnodes ("// price");

Foreach (xmlnode node in nodelist)

{

Node.innerxml = " Node.innertext " ";

}

This.TextBox1.text = xmldoc.outerxml;

}

Private void button8_click (Object Sender, System.EventArgs E)

{

Domopression Domoper = New Domoperation (this.xmlpath);

Try

{

THIS.TEXTBOX1.TEXT = Domoper.RemoveChildNode ("Company", "ProductFamily");

}

Catch (Exception Exp)

{

Messagebox.show (Exp.toT7tring (), "Error", MessageBoxButtons.ok, MessageBoxicon.Error);

}

}

}

}

The XML file used in the program is as follows:

Sample.xml

Black

l

32.99

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

New Post(0)