
这里写目录标题
一级目录
Demo
public static void XMLCreateAndWrite_2()
{
string xmlName = "King.xml";
XmlDocument xmlDoc = new XmlDocument();
XmlDeclaration xmlDec = xmlDoc.CreateXmlDeclaration("1.0", "UTF-8", null);
xmlDoc.AppendChild(xmlDec);
XmlElement section1 = xmlDoc.CreateElement("峡谷");
xmlDoc.AppendChild(section1);
XmlNode chapter1 = xmlDoc.CreateElement("法师");
section1.AppendChild(chapter1);
XmlElement example1 = xmlDoc.CreateElement("英雄");
example1.SetAttribute("名字", "嬴政");
example1.SetAttribute("皮肤颜色", "金色");
chapter1.AppendChild(example1);
XmlElement example2 = xmlDoc.CreateElement("英雄");
example2.SetAttribute("名字", "安琪啦");
example2.SetAttribute("皮肤颜色", "粉红色");
chapter1.AppendChild(example2);
XmlNode chapter2 = xmlDoc.CreateElement("射手");
section1.AppendChild(chapter2);
XmlElement example3 = xmlDoc.CreateElement("英雄");
example3.SetAttribute("名字", "后羿");
example3.SetAttribute("皮肤颜色", "金色");
chapter2.AppendChild(example3);
XmlElement example4 = xmlDoc.CreateElement("英雄");
example4.SetAttribute("名字", "李元芳");
example4.SetAttribute("皮肤颜色", "红色");
chapter2.AppendChild(example4);
xmlDoc.Save(xmlName);
StreamReader reader = File.OpenText(xmlName);
Console.Write(reader.ReadToEnd());
reader.Close();
Console.ReadLine();
}
效果
