unity(C#)对于XML文件的创建及添加

创建及添加XML文件:

string localPath = Application.streamingAssetsPath + "/" + "SchoolReport.xml";

if (!File.Exists(localPath))
        {
            XmlDocument xml = new XmlDocument();
            
            //想要添加XML文件的话将这行代码替换为     xml.Load(localPath);
            XmlDeclaration xmldecl = xml.CreateXmlDeclaration("1.0", "UTF-8", "");
            //创建根节点
            XmlElement root = xml.CreateElement("xml");
            //创建子节点
            XmlElement info = xml.CreateElement("scoreinfo");
            XmlElement info2 = xml.CreateElement("name");
            XmlElement info3 = xml.CreateElement("score");
            //给子节点添加属性
            //info.SetAttribute("id", "1");
            //给子节点写入数据
            info2.InnerText = "aaa";
            info3.InnerText = "100";
            
            
            //按顺序创建节点
            root.AppendChild(info);
            info.AppendChild(info2);
            info.AppendChild(info3);
            xml.AppendChild(root);
            //保存
            xml.Save(localPath);
            Debug.Log("创建XML成功!");

        }

猜你喜欢

转载自blog.csdn.net/weixin_51481499/article/details/122238976