效果:
代码:
foreach (XmlNode xn2 in xn.ChildNodes)
{
treeView1.TopNode.Nodes[i].Nodes.Add(xn2.InnerText);
}
i++;
}
}
public void CreateXML()
{
//创建xml文件
XmlDeclaration dec = xml.CreateXmlDeclaration("1.0", "utf-8", null);
xml.AppendChild(dec);
//添加树形字段
XmlElement One= xml.CreateElement("one");
XmlElement two = xml.CreateElement("two");
XmlElement two1 = xml.CreateElement("two");
XmlElement three = xml.CreateElement("three");
XmlElement threeDL = xml.CreateElement("three");
XmlElement three1 = xml.CreateElement("three");
XmlElement three10 = xml.CreateElement("three");
//添加树形结构关系
xml.AppendChild(one);
one.AppendChild(two);
one.AppendChild(two1);
two.AppendChild(three);
two.AppendChild(threeDL);
two1.AppendChild(three1);
two1.AppendChild(three10);
//添加属性
two.SetAttribute("two", "不死");
two1.SetAttribute("two", "暗夜");
//添加内容
three.InnerText = "DK";
threeDL.InnerText = "DL";
three1.InnerText = "DH";
three10.InnerText = "WD";
xml.Save(@"D:\XMLFile.xml");
}