简单的xml c#

<pre name="code" class="csharp">using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Mime;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            XMLTest._infos = new List<ObjectInfo>();

            XMLTest._infos.Add( new ObjectInfo { guid = "111", assetBundlePath = "aaaaa" } );
            XMLTest._infos.Add(new ObjectInfo { guid = "2222", assetBundlePath = "bbbb" });
            XMLTest._infos.Add(new ObjectInfo { guid = "33333", assetBundlePath = "cccccc" });
            XMLTest._infos.Add(new ObjectInfo { guid = "44444444", assetBundlePath = "ddddddd" });
            XMLTest._infos.Add(new ObjectInfo { guid = "555555555", assetBundlePath = "eeeeeee" });

            XMLTest.SaveToXML();

            XMLTest.ReadFromXML();

            System.Console.ReadLine();
        }
    }
}


[System.Serializable]
public class ObjectInfo
{
    public string guid;
    public string assetBundlePath;
}

#region xml读写

public class XMLTest
{
    static public List<ObjectInfo> _infos;

    public static void SaveToXML()
    {
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.CreateXmlDeclaration("1.0", "utf-8", "yes");

        XmlNode rootNode = xmlDoc.CreateElement("ObjectsInfo");
        xmlDoc.AppendChild(rootNode);

        foreach (var VARIABLE in _infos)
        {
            XmlNode node = xmlDoc.CreateElement("object");
            rootNode.AppendChild(node);

            var attribute = xmlDoc.CreateAttribute("guid");
            attribute.Value = VARIABLE.guid;
            node.Attributes.Append(attribute);

            attribute = xmlDoc.CreateAttribute("assetBundlePath");
            attribute.Value = VARIABLE.assetBundlePath.ToString();
            node.Attributes.Append(attribute);
        }

        FileStream fs = new FileStream(@"\ObjectsInScene.xml", FileMode.OpenOrCreate);
        fs.Close();

        xmlDoc.Save(@"\ObjectsInScene.xml");
    }

    static public float ToFloat(string s)
    {
        try
        {
            return float.Parse(s);
        }
        catch (Exception)
        {
            return 0;
        }
    }

    public static void ReadFromXML()
    {
        //if(!File.Exists(Application.streamingAssetsPath + @"\ObjectsInScene.xml")) return;

//        FileStream fs = new FileStream(@"\ObjectsInScene.xml", FileMode.OpenOrCreate);
//
//        if(fs.Length == 0) return;
//
//        _infos.Clear();

        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(@"\ObjectsInScene.xml");

        var rootNode = xmlDoc.ChildNodes[0];
        var nodeList = rootNode.ChildNodes;

        foreach (XmlNode VARIABLE in nodeList)
        {
            ObjectInfo info = new ObjectInfo();
            _infos.Add(info);
            info.guid = VARIABLE.Attributes["guid"].Value;
            info.assetBundlePath = VARIABLE.Attributes["assetBundlePath"].Value;
            Console.WriteLine("guid   {0},  path {1} ", info.guid, info.assetBundlePath);
        }
    }
}

#endregion


 
 

                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值