XML文件操作--创建

来源:互联网 发布:淘宝发布宝贝怎么预览 编辑:程序博客网 时间:2024/06/06 13:58

首先本例是在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);     }




0 0
原创粉丝点击