版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ZFSR05255134/article/details/46916467
首先本例是在Unity工程中实现的:
注意:要保存成无“BOM”的utf-8格式,否则在手机平台上无法读取。
头文件:
using System.Xml;
using System.IO;
using System.Text;
/// <summary>
/// 创建一个简单的XML文件
/// </summary>
private void createXml1()
{
XmlDocument xml = new XmlDocument();
///创建XML的声明
xml.AppendChild(xml.CreateXmlDeclaration("1.0", "utf-8", null));
///创建根节点
XmlElement root = xml.CreateElement("Root");
xml.AppendChild(root);
XmlElement nodes = xml.CreateElement("nodes");
XmlElement node1 = xml.CreateElement("node1");
node1.SetAttribute("abc", "abce");
node1.InnerText = "noname";
nodes.AppendChild(node1);
root.AppendChild(nodes);
string path = Application.dataPath + "/res/test.xml";
///////////////////////////////////////////////////////////////////////////////
/// 创建无BOM开头标示的XML文件
StreamWriter sw = new StreamWriter(path, false, new UTF8Encoding(false));
//保存xml到Unity工程目录中 Application.dataPath + "/res/test.xml";
xml.Save(sw);
sw.WriteLine();
sw.Close();
///////////////////////////////////////////////////////////////////////////////
//保存xml到Unity工程目录中 Application.dataPath + "/res/test.xml";
//xml.Save(path);
}