XmlTextWriter tw;
public void WriteXML(TreeNodeCollection tc)
{
foreach (TreeNode Node in tc)
{
WriteOneXML(Node);
WriteXML(Node.Nodes);
tw.WriteEndElement();
}
}
private void WriteOneXML(TreeNode Node)
{
string objid = Node.Tag.ToString();
string objName = Node.Text;
tw.WriteStartElement("cantoncode" + (Node.Level + 1));
tw.WriteAttributeString("cantoncode", objid);
tw.WriteAttributeString("cantonname", objName);
}
private void button3_Click(object sender, EventArgs e)
{
string fileName = @"C:/cantoncode.xml";
tw = new XmlTextWriter(fileName, null);
tw.Formatting = Formatting.Indented;
tw.WriteStartDocument();
tw.WriteStartElement("cantoncode");
WriteXML(treeView1.Nodes);
tw.WriteEndElement();
tw.WriteEndDocument();
tw.Flush();
tw.Close();
}