Using System;
Using System. Collections. Generic;
Using System. ComponentModel;
Using System. Data;
Using System. Drawing;
Using System. Linq;
Using System. Text;
Using System. Windows. Forms;
Using System. Diagnostics;
Using System. Xml;
Namespace read/write xml Test
{
Public partial class Form1: Form
{
String xmlPath = Application. StartupPath + "\ demo. xml ";
XmlElement xmlroot;
XmlDocument xmldoc = new XmlDocument ();
Public Form1 ()
{
InitializeComponent ();
}
* ****** Void button#click (object sender, EventArgs e)
{
// Add the xml Root Node
Xmlroot = xmldoc. CreateElement (textBox1.Text );
Xmldoc. AppendChild (xmlroot );
Xmldoc. Save (xmlPath );
}
* ****** Void button2_Click (object sender, EventArgs e)
{
// Add a child node of XML
Xmlelement xmlchild1 = xmldoc. createelement (textbox2.text );
Xmlchild1.innertext = "red ";
Xmlroot. appendchild (xmlchild1 );
Xmldoc. Save (xmlpath );
}
* ****** Void button3_click (Object sender, eventargs E)
{
// Traverse display
String str1 = null;
XmlDocument myxml = new XmlDocument ();
Myxml. Load (xmlPath );
XmlNode rootnode = myxml. DocumentElement;
Foreach (XmlNode m in rootnode. ChildNodes)
{
Str1 + = m. Name;
Str1 + = ":";
Str1 + = m. InnerText;
Str1 + = Environment. NewLine;
}
RichTextBox1.Text = str1;
}
}
}
Source code download