加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

XML文件操作--创建

发布时间:2020-12-16 09:03:03 所属栏目:百科 来源:网络整理
导读:首先本例是在Unity工程中实现的: 注意:要保存成无“BOM”的utf-8格式,否则在手机平台上无法读

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读