真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

C#XML與二進(jìn)制相互轉(zhuǎn)換

關(guān)于為什么需要轉(zhuǎn)換:本人步入Game行業(yè)已經(jīng)4年了,但是配置文件要麼是原生的XML文件,要麼是別人的二進(jìn)制文件.關(guān)于配置文件為啥要轉(zhuǎn)換成二進(jìn)制文件:主要是為了保密,其次才是節(jié)省空間.但是話(huà)又說(shuō)回來(lái)了,使用二進(jìn)制文件的時(shí)候,獲取信息,需要多一步轉(zhuǎn)化過(guò)程.

創(chuàng)新互聯(lián)公司專(zhuān)注于凌海企業(yè)網(wǎng)站建設(shè),成都響應(yīng)式網(wǎng)站建設(shè)公司,購(gòu)物商城網(wǎng)站建設(shè)。凌海網(wǎng)站建設(shè)公司,為凌海等地區(qū)提供建站服務(wù)。全流程定制網(wǎng)站,專(zhuān)業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,創(chuàng)新互聯(lián)公司專(zhuān)業(yè)和態(tài)度為您提供的服務(wù)

再者,在一個(gè)Game項(xiàng)目中可能有多個(gè)配置文件,本人目前在開(kāi)發(fā)的有100多個(gè),那么打包成ini二進(jìn)制是很有必要的.

來(lái)個(gè)例子:

XMLToBin : XML 與 二進(jìn)制文件的相互轉(zhuǎn)換

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
{
    /// 
    /// XML的格式轉(zhuǎn)換
    /// 
    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;
            }
        }
        /// 
        /// Object to XML
        /// 
        /// 
        /// 
        /// 
        /// 
        public bool Serializer(object obj, string path)
        {
            FileStream xmlfile = new FileStream(path, FileMode.OpenOrCreate);

            //創(chuàng)建序列化對(duì)象 
            XmlSerializer xml = new XmlSerializer(typeof(T));
            try
            {    //序列化對(duì)象
                xml.Serialize(xmlfile, obj);
                xmlfile.Close();
            }
            catch (InvalidOperationException)
            {
                throw;
            }

            return true;

        }
        /// 
        /// XML to Object
        /// 
        /// 
        /// 
        /// 
        public static T Deserializer(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
            {

            }
        }
        /// 
        /// Object to Bin
        /// 
        /// 
        /// 
        /// 
        public bool BinarySerializer(object obj, string path)
        {
            FileStream Stream = new FileStream(path, FileMode.OpenOrCreate);
            //創(chuàng)建序列化對(duì)象 
            BinaryFormatter bin = new BinaryFormatter();
            try
            {    //序列化對(duì)象
                bin.Serialize(Stream, obj);
                Stream.Close();
            }
            catch (InvalidOperationException)
            {
                throw;
            }
            return true;
        }
        /// 
        /// Bin to Object
        /// 
        /// 
        /// 
        /// 
        public T BinaryDeserializer(string path)
        {
            try
            {
                FileStream binfile = new FileStream(path, FileMode.Open);

                BinaryFormatter bin = new BinaryFormatter();
                //序列化對(duì)象
                //xmlfile.Close();
                T t = (T)bin.Deserialize(binfile);
                binfile.Close();
                return t;
            }
            catch (InvalidOperationException)
            {
                throw;
            }
            catch (FileNotFoundException)
            { throw; }
            finally
            {

            }
        }
        /// 
        /// 讀取文本
        /// 
        /// 
        /// 
        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"))) // 解決中文亂碼問(wèn)題
                {
                    string readStr;
                    while ((readStr = sr.ReadLine()) != null)
                    {
                        bcStr = bcStr + readStr;
                    }
                    sr.Close();
                }
                return bcStr;
            }
            else
            {
                Console.WriteLine("Message , 沒(méi)有找到文件{0}", targetPath);
                return string.Empty;
            }
        }
    }
}

family.xml:



  
    
  
  
    
  
  
    
  

測(cè)試:

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 -- 轉(zhuǎn)換成二進(jìn)制文件成功:{0}", XMLToBin.Instance.BinarySerializer(xml, "../../res/family.ini"));
            //string json = "{peopel={ [ husband={ name=\"ainy\" , age = \"26\" }, wife={ name=\"snow\" , age = \"24\" } ] }}";
            //Console.WriteLine("Message -- 轉(zhuǎn)換成二進(jìn)制文件成功:{0}", XMLToBin.Instance.BinarySerializer(json, "../../res/familyJson.ini"));
            Console.WriteLine("family.ini : {0}", XMLToBin.Instance.ReadCommon("../../res/family.ini"));
            string aXml = XMLToBin.Instance.BinaryDeserializer("../../res/family.ini");
            Console.WriteLine("Message -- 轉(zhuǎn)換成文本文件成功:{0}",aXml );
            Console.ReadKey();
        }
    }
}

注意到:其實(shí)Json和XML都可以.結(jié)果:

C#XML與二進(jìn)制相互轉(zhuǎn)換

看結(jié)果,中文的話(huà)都改變了,英文還隱隱約約看得到配置信息.目前就這樣了,畢竟中國(guó)游戲配置一大片都是中文的.另外還要感謝萬(wàn)能的技術(shù)論壇,一部分代碼是看來(lái)自:http://www.cnblogs.com/jesszhu/archive/2013/08/22/3276556.html

如果讀者有更好的方法,請(qǐng)不靈賜教.

附件:http://down.51cto.com/data/2367316

分享名稱(chēng):C#XML與二進(jìn)制相互轉(zhuǎn)換
新聞來(lái)源:http://weahome.cn/article/geesig.html

其他資訊

在線咨詢(xún)

微信咨詢(xún)

電話(huà)咨詢(xún)

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部