最近2018中文字幕在日韩欧美国产成人片_国产日韩精品一区二区在线_在线观看成年美女黄网色视频_国产精品一区三区五区_国产精彩刺激乱对白_看黄色黄大色黄片免费_人人超碰自拍cao_国产高清av在线_亚洲精品电影av_日韩美女尤物视频网站

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(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é)省空間.但是話又說回來了,使用二進(jìn)制文件的時候,獲取信息,需要多一步轉(zhuǎn)化過程.

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

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

來個例子:

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)建序列化對象 
            XmlSerializer xml = new XmlSerializer(typeof(T));
            try
            {    //序列化對象
                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)建序列化對象 
            BinaryFormatter bin = new BinaryFormatter();
            try
            {    //序列化對象
                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();
                //序列化對象
                //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"))) // 解決中文亂碼問題
                {
                    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:



  
    
  
  
    
  
  
    
  

測試:

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();
        }
    }
}

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

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

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

如果讀者有更好的方法,請不靈賜教.

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

分享名稱:C#XML與二進(jìn)制相互轉(zhuǎn)換
分享路徑:http://fisionsoft.com.cn/article/geesig.html