<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
简单的xml c#
最新推荐文章于 2024-09-04 19:16:19 发布