C # demo program for reading and writing XML (1)

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.