This is mainly about XmlDocument creating, deleting, editing, and inserting XML operations into nodes.
The above is simply created, the following example, XmlDocument create XML document and add Delete update node
using System;
Using System.Collections.Generic;
Using System.ComponentModel;
Using System.Data;
Using System.Drawing;
Using System.Text;
Using System.Windows.Forms;
Using System.Xml;
Namespace Xmldomdemo
{
public partial class Form1:form
{
Public Form1 ()
{
InitializeComponent ();
}
private void Btnload_click (object sender, EventArgs e)
& nbsp; {
XmlDocument xmldoc = new XmlDocument ();
xmldoc.load ("books.xml");
MessageBox.Show (xmldoc.innerxml);
}
//Create document
private void Btncreate_click (object sender, EventArgs e)
{
XmlDocument xmldoc = new XmlDocument ();
Create a definition declaration for XML
XmlDeclaration Dec = xmldoc.createxmldeclaration ("1.0", "gb2312", null);
Xmldoc.appendchild (DEC);
To create a root node
XmlElement root = Xmldoc.createelement ("books");
Xmldoc.appendchild (root);
XmlNode book = xmldoc.createelement ("book");
XmlElement title = xmldoc.createelement ("title");
Title.innertext = "SQL Server";
Book.appendchild (title);
XmlElement ISBN = xmldoc.createelement ("ISBN");
Isbn.innertext = "444444";
Book.appendchild (ISBN);
XmlElement author = xmldoc.createelement ("author");
Author.innertext = "Jia";
Book.appendchild (author);
XmlElement price = Xmldoc.createelement ("price");
Price.innertext = "120";
Price.setattribute ("unit", "___fckpd___0quot;");
Book.appendchild (price);
Root.appendchild (book);
Xmldoc.save ("books.xml");
}
private void Btninsert_click (object sender, EventArgs e)
{
XmlDocument xmldoc = new XmlDocument ();
Xmldoc.load ("books.xml");
XmlNode root = Xmldoc.selectsinglenode ("books");
XmlElement book = xmldoc.createelement ("book");
XmlElement title = xmldoc.createelement ("title");
Title.innertext = "xml";
Book.appendchild (title);
XmlElement ISBN = xmldoc.createelement ("ISBN");
Isbn.innertext = "333333";
Book.appendchild (ISBN);
XmlElement author = xmldoc.createelement ("author");
Author.innertext = "Snow";
Book.appendchild (author);
XmlElement price = Xmldoc.createelement ("price");
Price.innertext = "120";
Price.setattribute ("unit", "___fckpd___0quot;");
Book.appendchild (price);
Root.appendchild (book);
Xmldoc.save ("books.xml");
MessageBox.Show ("Data has been written!") ");
}
private void Btnupdate_click (object sender, EventArgs e)
{
XmlDocument xmldoc = new XmlDocument ();
Xmldoc.load ("books.xml");
//"//book[@unit =" $ "]"
//Get all child nodes of the books node
XmlNodeList nodelist = Xmldoc.selectsinglenode ("Books//book"). ChildNodes;
//Traverse all child nodes
foreach (XmlNode xn in nodelist)
{
//Convert child node type to XmlElement type
XmlElement XE = (XmlElement) xn;
if (xe.name = = " Author ")
{
Xe.innertext = "Amandag";
}
if (Xe.getattribute ("unit") = = "___fckpd___0quot;)
{
Xe.setattribute ("unit", "¥");
}
Break
}
XmlNodeList nodelist = xmldoc.selectnodes ("Books//book");
foreach (XmlNode xn in nodelist)
//{
foreach (XmlNode x in Xn.childnodes)
// {
To convert a child node type to a XmlElement type
XmlElement XE = (XmlElement) x;
if (Xe.name = = "Author")
// {
Xe.innertext = "Amandag";
// }
if (Xe.getattribute ("unit") = = "___fckpd___0quot;)
// {
Xe.setattribute ("unit", "¥");
// }
break;
// }
//}
Xmldoc.save ("books.xml");
}
private void Btndelete_click (object sender, EventArgs e)
{
XmlDocument xmldoc = new XmlDocument ();
Xmldoc.load ("books.xml");
XmlNodeList nodelist = Xmldoc.selectsinglenode ("Books//book"). ChildNodes;
//Traverse all child nodes
foreach (XmlNode xn in nodelist)
{
//Convert child node type to XmlElement type
XmlElement XE = (XmlElement) xn;
if (xe.name = = " Author ")
{
Xe.removeall ();
}
if ( Xe.getattribute ("unit") = = "¥")
{
xe.removeattribute ("unit");
}
}
xmldoc.save ("books.xml");
}
}
}