Preface
We know that we can use JavaScript to operate the html dom structure to complete some functions. In. NET, we can also use C # To operate the xml dom structure to complete some functions. The following is an example.
Reading directory
1. Implementation steps
Ii. Running Effect
Iii. extended learning
Instance
I:Steps
1: Write an XML file
I take the book Mall as an example. Here is an XML document illustration written using four famous books in China:
1.1 XMLFile1.xml
2: code file writing
2.1 Form1.cs
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 DOM
{
Public partial class Form1: Form
{
Public Form1 ()
{
InitializeComponent ();
}
Private void Form1_Load (object sender, EventArgs e)
{
}
/// <Summary>
/// Read the xml file using the XmlDocument object
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "e"> </param>
Private void button#click (object sender, EventArgs e)
{
This. listBox1.Items. Clear ();
String strFileName = "XMLFile1.xml ";
XmlDocument xmldoc = new XmlDocument ();
Xmldoc. Load (strFileName );
XmlNodeList nodelist = xmldoc. GetElementsByTagName ("Name ");
XmlNodeList nodelist2 = xmldoc. GetElementsByTagName ("Author ");
XmlNodeList nodelist3 = xmldoc. GetElementsByTagName ("Money ");
Foreach (XmlNode node in nodelist)
{
This. listBox1.Items. Add (node. InnerText );
}
Foreach (XmlNode node in nodelist2)
{
This. listBox2.Items. Add (node. InnerText );
}
Foreach (XmlNode node in nodelist3)
{
This. listBox3.Items. Add (node. InnerText );
}
}
/// <Summary>
/// Insert a new node
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "e"> </param>
Private void button2_Click (object sender, EventArgs e)
{
String strFileName = "XMLFile1.xml ";
XmlDocument xmldoc = new XmlDocument ();
Xmldoc. Load (strFileName );
// Create the <Book/> element
XmlElement xe = xmldoc. CreateElement ("Book ");
Xe. SetAttribute ("Number", "product-005 ");
// Create the <Name/> child element in the <Book/> element
XmlElement xe_name = xmldoc. CreateElement ("Name ");
Xe_name.InnerText = "Liang Jian";
Xe. AppendChild (xe_name );
// Create the <OthorName/> child element in the <Book/> element
XmlElement xe_othorname = xmldoc. CreateElement ("OthorName ");
Xe_othorname.InnerText = "Tie blood army soul ";
Xe. AppendChild (xe_othorname );
// Create the <Author/> child element in the <Book/> element
XmlElement xe_author = xmldoc. CreateElement ("Author ");
Xe_author.InnerText = "zhangyi ";
Xe. AppendChild (xe_author );
// Create the <Money/> child element in the <Book/> element
XmlElement xe_money = xmldoc. CreateElement ("Money ");
Xe_money.innetext = "150 ";
Xe. AppendChild (xe_money );
// Create the <Issue/> child element in the <Book/> element
XmlElement xe_issue = xmldoc. CreateElement ("Issue ");
Xe_issue.InnerText = "Mechanical Industry Press ";
Xe. AppendChild (xe_issue );
// Add the <Book/> element
Xmldoc. DocumentElement. AppendChild (xe );
// Write the XML document
XmlTextWriter writer = new XmlTextWriter ("XMLFile1.xml", null );
Xmldoc. WriteContentTo (writer );
Writer. Close ();
// Read from the XML document
This. listBox1.Items. Clear ();
XmlNodeList nodelist = xmldoc. GetElementsByTagName ("Name ");
XmlNodeList nodelist2 = xmldoc. GetElementsByTagName ("Author ");
XmlNodeList nodelist3 = xmldoc. GetElementsByTagName ("Money ");
Foreach (XmlNode node in nodelist)
{
This. listBox1.Items. Add (node. InnerText );
}
Foreach (XmlNode node in nodelist2)
{
This. listBox2.Items. Add (node. InnerText );
}
Foreach (XmlNode node in nodelist3)
{
This. listBox3.Items. Add (node. InnerText );
}
}
/// <Summary>
/// View all information of the selected title
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "e"> </param>
Private void listBox1_SelectedIndexChanged (object sender, EventArgs e)
{
String strFileName = "XMLFile1.xml ";
XmlDocument xmldoc = new XmlDocument ();
Xmldoc. Load (strFileName );
String strSecarch = "BookShops/Book [Name = '" + this. listBox1.SelectedItem. ToString () + "']";
XmlNode node = xmldoc. SelectSingleNode (strSecarch );
If (node! = Null)
{
MessageBox. Show (node. InnerText );
}
Else
{
MessageBox. Show ("not found ");
}
}
}
}
Ii. Running Effect
After clicking the "read Xml using XmlDocument object" button, the effect is as follows:
When we click "insert node", we find that the content of our XMLFile1.xml document has been changed. When we read the XMLFile1.xml document again, the effect is as follows:
When we select the title of the book, a prompt will pop up to get all the information of the book.
Iii. extended learning
In my article titled "C # the XML programming of Zhixin Learning Series-Xml writer XmlWriter class (3, "Combined with" that area also achieves the ability to write XML document elements, attributes, and text. What is the difference between writing XML documents here? Think about it yourself