public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void xmlCreat()
{
XmlDocument doc = new XmlDocument();
XmlDeclaration xmldec = doc.CreateXmlDeclaration("1.0","UTF-8",null);
doc.AppendChild(xmldec);
XmlElement e1 = doc.CreateElement("points");
e1.SetAttribute("Name","aa");
e1.InnerText = "a test";
doc.AppendChild(e1);
XmlElement e11 = doc.CreateElement("point1");
e11.InnerText = "p1";
XmlElement e12 = doc.CreateElement("point2");
e12.InnerText = "p2";
e1.AppendChild(e11);
e1.AppendChild(e12);
doc.Save(Application.StartupPath + "\\test.xml");
}
public void ReadXml()
{
if (File.Exists("test.xml"))
{
XmlDocument doc = new XmlDocument();
doc.Load("test.xml");
XmlElement roote = doc.DocumentElement;
textBox1.AppendText (roote.GetAttribute("Name") + "\r\n");
textBox1.AppendText(roote.InnerText + "\r\n");
textBox1.AppendText(roote.SelectSingleNode("/points/point1").InnerText);
}
}
private void button1_Click(object sender, EventArgs e)
{
xmlCreat();
}
private void button2_Click(object sender, EventArgs e)
{
ReadXml();
}
}