关于为什么需要转换:本人步入Game行业已经4年了,但是配置文件要麽是原生的XML文件,要麽是别人的二进制文件.关于配置文件为啥要转换成二进制文件:主要是为了保密,其次才是节省空间.但是话又说回来了,使用二进制文件的时候,获取信息,需要多一步转化过程.
公司主营业务:网站设计制作、成都做网站、移动网站开发等业务。帮助企业客户真正实现互联网宣传,提高企业的竞争能力。成都创新互联是一支青春激扬、勤奋敬业、活力青春激扬、勤奋敬业、活力澎湃、和谐高效的团队。公司秉承以“开放、自由、严谨、自律”为核心的企业文化,感谢他们对我们的高要求,感谢他们从不同领域给我们带来的挑战,让我们激情的团队有机会用头脑与智慧不断的给客户带来惊喜。成都创新互联推出横峰免费做网站回馈大家。再者,在一个Game项目中可能有多个配置文件,本人目前在开发的有100多个,那么打包成ini二进制是很有必要的.
来个例子:
XMLToBin : XML 与 二进制文件的相互转换
family.xml : XML文件
XMLToBin:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; using System.Xml.Serialization; using System.Runtime.Serialization.Formatters.Binary; namespace XmlToByte.ainy { /// <summary> /// XML的格式转换 /// </summary> public class XMLToBin { //public string a = "a"; private static XMLToBin instance; public static XMLToBin Instance { get { if (XMLToBin.instance == null) { XMLToBin.instance = new XMLToBin(); } return XMLToBin.instance; } set { XMLToBin.instance = value; } } public XMLToBin() { if (XMLToBin.instance != null) { InstanceNoInstantiationException exp = new InstanceNoInstantiationException(typeof(XMLToBin)); Console.WriteLine( exp.Message); throw exp; } else { XMLToBin.instance = this; } } /// <summary> /// Object to XML /// </summary> /// <typeparam name="T"></typeparam> /// <param name="obj"></param> /// <param name="path"></param> /// <returns></returns> public bool Serializer<T>(object obj, string path) { FileStream xmlfile = new FileStream(path, FileMode.OpenOrCreate); //创建序列化对象 XmlSerializer xml = new XmlSerializer(typeof(T)); try { //序列化对象 xml.Serialize(xmlfile, obj); xmlfile.Close(); } catch (InvalidOperationException) { throw; } return true; } /// <summary> /// XML to Object /// </summary> /// <typeparam name="T"></typeparam> /// <param name="path"></param> /// <returns></returns> public static T Deserializer<T>(string path) { try { FileStream xmlfile = new FileStream(path, FileMode.Open); XmlSerializer xml = new XmlSerializer(typeof(T)); T t = (T)xml.Deserialize(xmlfile); xmlfile.Close(); return t; } catch (InvalidOperationException) { throw; } catch (FileNotFoundException) { throw; } finally { } } /// <summary> /// Object to Bin /// </summary> /// <param name="obj"></param> /// <param name="path"></param> /// <returns></returns> public bool BinarySerializer(object obj, string path) { FileStream Stream = new FileStream(path, FileMode.OpenOrCreate); //创建序列化对象 BinaryFormatter bin = new BinaryFormatter(); try { //序列化对象 bin.Serialize(Stream, obj); Stream.Close(); } catch (InvalidOperationException) { throw; } return true; } /// <summary> /// Bin to Object /// </summary> /// <typeparam name="T"></typeparam> /// <param name="path"></param> /// <returns></returns> public T BinaryDeserializer<T>(string path) { try { FileStream binfile = new FileStream(path, FileMode.Open); BinaryFormatter bin = new BinaryFormatter(); //序列化对象 //xmlfile.Close(); T t = (T)bin.Deserialize(binfile); binfile.Close(); return t; } catch (InvalidOperationException) { throw; } catch (FileNotFoundException) { throw; } finally { } } /// <summary> /// 读取文本 /// </summary> /// <param name="targetPath"></param> /// <returns></returns> public string ReadCommon(string targetPath) { if (File.Exists(targetPath)) { //using (StreamReader sr = File.OpenText(targetPath)) // 读中文将乱码 string bcStr = ""; using (StreamReader sr = new StreamReader(targetPath, UnicodeEncoding.GetEncoding("GB2312"))) // 解决中文乱码问题 { string readStr; while ((readStr = sr.ReadLine()) != null) { bcStr = bcStr + readStr; } sr.Close(); } return bcStr; } else { Console.WriteLine("Message , 没有找到文件{0}", targetPath); return string.Empty; } } } }
family.xml:
<?xml version="1.0" encoding="utf-8" ?> <people> <husband> <attribute name ="胡Ainy" age ="26" ></attribute> </husband> <wife> <attribute name ="snow" age="24"></attribute> </wife> <daughter> <attribute name ="ms" age="1"></attribute> </daughter> </people>
测试:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using XmlToByte.ainy; namespace XmlToByte { class Program { static void Main(string[] args) { string xml = XMLToBin.Instance.ReadCommon("../../res/family.xml"); Console.WriteLine("family.xml : {0}", xml); Console.WriteLine("Message -- 转换成二进制文件成功:{0}", XMLToBin.Instance.BinarySerializer(xml, "../../res/family.ini")); //string json = "{peopel={ [ husband={ name=\"ainy\" , age = \"26\" }, wife={ name=\"snow\" , age = \"24\" } ] }}"; //Console.WriteLine("Message -- 转换成二进制文件成功:{0}", XMLToBin.Instance.BinarySerializer(json, "../../res/familyJson.ini")); Console.WriteLine("family.ini : {0}", XMLToBin.Instance.ReadCommon("../../res/family.ini")); string aXml = XMLToBin.Instance.BinaryDeserializer<string>("../../res/family.ini"); Console.WriteLine("Message -- 转换成文本文件成功:{0}",aXml ); Console.ReadKey(); } } }
注意到:其实Json和XML都可以.结果:
看结果,中文的话都改变了,英文还隐隐约约看得到配置信息.目前就这样了,毕竟中国游戏配置一大片都是中文的.另外还要感谢万能的技术论坛,一部分代码是看来自:http://www.cnblogs.com/jesszhu/archive/2013/08/22/3276556.html
如果读者有更好的方法,请不灵赐教.
附件:http://down.51cto.com/data/2367316另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
文章名称:C#XML与二进制相互转换-创新互联
地址分享:https://www.cdcxhl.com/article24/diihce.html
成都网站建设公司_创新互联,为您提供定制开发、动态网站、品牌网站制作、品牌网站建设、网站策划、网站营销
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联