经常用到xml,用代码创建xml,
void CreateXML () {
XmlDocument oXml = new XmlDocument();
//XML 声明
oXml.AppendChild(oXml.CreateXmlDeclaration("1.0", "UTF-8", null));
//根节点
XmlElement root = oXml.CreateElement("Root");
oXml.AppendChild(root);
XmlElement nodes = oXml.CreateElement("nodes");
XmlElement nodes1 = oXml.CreateElement("nodes1");
nodes1.SetAttribute("abc", "abcd");
nodes1.SetAttribute("abc2", "abcd2");
nodes1.InnerText = "noname";
nodes.AppendChild(nodes1);
root.AppendChild(nodes);
string path = Application.dataPath + "/res/test.xml";
StreamWriter sw = new StreamWriter(path, false, new UTF8Encoding(false));
oXml.Save(sw);
sw.WriteLine();
sw.Close();
}
结果如图: