I am writing something about XML today ., XML
Using System;
Using System. Windows. Forms;
Using System. Xml;
Namespace winformDemo
{
Public partial class Form2: Form
{
Public Form2 ()
{
InitializeComponent ();
}
// XmlDocument open the document
XmlDocument xc = new XmlDocument ();
/// <Summary>
/// Display the names of all items
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "e"> </param>
Private void button#click (object sender, EventArgs e)
{
This. textBox1.Text = "";
// Xc. Load (write the path of the document to be retrieved)
Xc. Load ("D: \ Text \ shopping. xml ");
// XmlNodeList xnl = xc. SelectSingleNode ("shopping"). ChildNodes; get the subnodes under shopping and put them in the set.
XmlNodeList xnl = xc. SelectSingleNode ("shopping"). ChildNodes;
// Cyclically fetch sub-nodes
Foreach (XmlNode xn in xnl)
{
// Obtain the product name.
// This. textBox1.Text + = xn. Attributes ["id"]. Value + "\ r \ n ";
Foreach (XmlNode xnd in xn. ChildNodes)
{
If (xnd. Name = "Shopname ")
{
TextBox1.Text + = "Product Name:" + xnd. InnerText + "\ r \ n ";
}
}
}
}
/// <Summary>
/// Display all product information
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "e"> </param>
Private void button2_Click (object sender, EventArgs e)
{
This. textBox2.Text = "";
// Xc. Load (write the path of the document to be retrieved)
Xc. Load ("D: \ Text \ shopping. xml ");
// XmlNodeList xnl = xc. SelectSingleNode ("shopping"). ChildNodes; get the subnodes under shopping and put them in the set.
XmlNodeList xnl = xc. SelectSingleNode ("shopping"). ChildNodes;
// XmlNodeList xnl = xc. SelectSingleNode ("/shopping/shop [last ()]"). ChildNodes;
// XmlNodeList xnl = xc. SelectSingleNode ("/shopping/shop [@ id = 2]"). ChildNodes;
// Cyclically fetch sub-nodes
Foreach (XmlNode xn in xnl)
{
// Obtain the last data record.
// TextBox2.Text + = "Product Name:" + xn. InnerText + "\ r \ n ";
// Obtain information about all products.
TextBox2.Text + = "item ID:" + xn. Attributes ["id"]. Value + "\ r \ n ";
TextBox2.Text + = "Product Name:" + xn. ChildNodes [0]. InnerText + "\ r \ n ";
TextBox2.Text + = "product price:" + xn. ChildNodes [1]. InnerText + "\ r \ n ";
TextBox2.Text + = "item quantity:" + xn. ChildNodes [2]. InnerText + "\ r \ n ";
}
}
/// <Summary>
/// Read all information
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "e"> </param>
Private void button3_Click (object sender, EventArgs e)
{
This. textBox3.Text = "";
XmlReader reader = XmlReader. Create (@ "D: \ Text \ shopping. xml ");
While (reader. Read ())
{
Switch (reader. NodeType)
{
Case XmlNodeType. Attribute:
For (int I = 0; I <reader. AttributeCount; I ++)
{
Reader. MoveToAttribute (I );
This. textBox3.Text + = "Property:" + reader. Name + "=" + reader. Value + "\ r \ n ";
}
Break;
Case XmlNodeType. Element:
This. textBox3.Text + = "node name:" + reader. LocalName + "\ r \ n ";
For (int I = 0; I <reader. AttributeCount; I ++)
{
Reader. MoveToAttribute (I );
This. textBox3.Text + = "Property:" + reader. Name + "=" + reader. Value + "\ r \ n ";
}
Break;
Case XmlNodeType. Text:
This. textBox3.Text + = "node content:" + reader. Value + "\ r \ n ";
Break;
}
}
}
/// <Summary>
/// Add information
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "e"> </param>
Private void button4_Click (object sender, EventArgs e)
{
XmlWriter writer = XmlWriter. Create (@ "D: \ Text \ goods. xml ");
Writer. WriteStartElement ("dog ");
Writer. WriteAttributeString ("color", "black ");
Writer. WriteString ("How can I be sure ");
Writer. WriteEndDocument ();
Writer. Close ();
}
}
}
Adding XML is directly added and not displayed. So do not click it again. You can read it in the text.
Summary:
XML
Xml:
(Super Markup Language) also known as (Extensible Markup Language) (simple data storage language) (Data Storage Technology)
Format:
Gb2312, UTF-8;
The database is stored in XML format.
Features:
Cross-platform, cross-language, cross-system, cross-browser, custom node name, closed, case sensitive, custom attribute, and unique root node.
C # provides XML objects and methods:
XMLDocument (xml document): (load (PATH), SelectSingleNode (get root node), childNodes (subnode ))
XMLNodelist (node set) :( XmlNodeList list = nodes under the root node)
XMLNode (node): (attributes: text, innerText, name, attributrs [], value, childNodes)