如何将XML文件转换为二进制文件

时间:2015-02-01 04:56:28

标签: c# xml binary

如何将XML文件转换为二进制文件(BXML)以隐藏其内容以进行分发?

我使用CryptoBinary / DecrytBinary来隐藏XML文件。 但是,我想直接将XML文件转换为二进制文件。

    ///-----------------------------------------------------------------
    /// Using part
    private void GetBookInfo()
    {
        //Books XML file
        Utility.XmlDeSerializer<CBooks>(ref Uinst, Environment.CurrentDirectory + "\\Book.xml");
        // Books Binary file
        string binXmlString = Utility.ReadCryptoBinaryFile(Environment.CurrentDirectory + "\\Book.bin");
        Utility.XmlStringToObject<CBooks>(ref Uinst, binXmlString);
    }

    private void SetBookInfo()
    {
        // Books XML file
        Utility.XmlSerializer<CBooks>(Uinst, Environment.CurrentDirectory + "\\Book.xml");
        //For test. for Books Binary file
        string binXmlString = Utility.ObjectToXmlString<CBooks>(Uinst);
        Utility.WriteCryptoBinaryFile(Environment.CurrentDirectory + "\\Book.bin", binXmlString);
    }




    ///-----------------------------------------------------------------
    /// Utility part
    public static void XmlDeSerializer<T>(ref T t, string path)
    {
        try
        {
            XmlSerializer serializer = new XmlSerializer(t.GetType());
            using (StreamReader Rstream = new StreamReader(path))
            {
                t = (T)serializer.Deserialize(Rstream);
                Rstream.Close();
            }
        }
        catch (Exception ex)
        {
            throw new InvalidOperationException("Failed to create object from xml string\r\n{0}", ex.InnerException);
        }
    }

    public static string ReadCryptoBinaryFile(string path)
    {
        FileStream stream = null;
        StreamReader reader = null;
        try
        {
            stream = new FileStream(path, FileMode.Open, FileAccess.Read);

            DESCryptoServiceProvider cryptic = new DESCryptoServiceProvider();

            cryptic.Key = Encoding.UTF8.GetBytes(Value.sKey);
            cryptic.IV = Encoding.UTF8.GetBytes(Value.sIV);

            CryptoStream crStream = new CryptoStream(stream,
                cryptic.CreateDecryptor(), CryptoStreamMode.Read);

            //reader = new StreamReader(crStream);
            //rreturn reader.ReadToEnd();
            BinaryReader br = new BinaryReader(crStream);
            return Encoding.UTF8.GetString(br.ReadBytes((int)stream.Length));
        }
        #region
        catch (Exception ex)
        {
            throw new IOException(string.Format("Message: {0}", ex));
        }
        finally
        {
            if (reader != null)
            {
                reader.Close();
            }
            if (stream != null)
            {
                /*stream.Flush(); */
                stream.Close();
            }
        }
        #endregion
    }
    public static void XmlStringToObject<T>(ref T obj, string xmlString)
    {
        try
        {
            XmlSerializer xs = new XmlSerializer(obj.GetType());
            MemoryStream ms = new MemoryStream(StringToBytes(xmlString, Encoding.UTF8));
            XmlTextWriter xmlTextWriter = new XmlTextWriter(ms, Encoding.UTF8);
            obj = (T)xs.Deserialize(ms);
        }
        catch (Exception ex)
        {
            throw new InvalidOperationException("Failed to " + "create object from xml string", ex);
        }
    }
    public static void XmlSerializer<T>(T obj, string path)
    {
        try
        {                
            XmlSerializer xmlserializer = new XmlSerializer(obj.GetType());
            using (StreamWriter Wstream = new StreamWriter(path))
            {
                xmlserializer.Serialize(Wstream, obj);
                Wstream.Flush(); Wstream.Close();
            }
        }
        catch (Exception ex)
        {
            throw new InvalidOperationException("Failed to create xml file from object\r\n{0}", ex);
        }
    }

    public static string ObjectToXmlString<T>(T obj)
    {
        try
        {
            string xmlString = null;
            MemoryStream ms = new MemoryStream();
            XmlSerializer xs = new XmlSerializer(obj.GetType());
            XmlTextWriter xtw = new XmlTextWriter(ms, Encoding.UTF8);
            xs.Serialize(xtw, obj);
            ms = (MemoryStream)xtw.BaseStream;
            //xtw.WriteBinHex(ms.ToArray(), 0, (int)ms.Length);
            xmlString = BytesToString(ms.ToArray(), Encoding.UTF8); 
            return xmlString;
        }
        catch (Exception ex)
        {
            throw new InvalidOperationException("Failed to " + "create xml string from object", ex);
        }
    }

    public static void WriteCryptoBinaryFile(string path, string data)
    {
        FileStream stream = null;
        CryptoStream crStream = null;
        try
        {
            stream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write);

            DESCryptoServiceProvider cryptic = new DESCryptoServiceProvider();

            cryptic.Key = Encoding.UTF8.GetBytes(Value.sKey);
            cryptic.IV = Encoding.UTF8.GetBytes(Value.sIV);

            crStream = new CryptoStream(stream,
               cryptic.CreateEncryptor(), CryptoStreamMode.Write);

            //byte[] bt = Encoding.UTF8.GetBytes(data);
            //crStream.Write(bt, 0, bt.Length);
            BinaryWriter bw = new BinaryWriter(crStream);
            bw.Write(Encoding.UTF8.GetBytes(data));
        }
        #region
        catch (Exception ex)
        {
            throw new IOException(string.Format("Message: {0}", ex));
        }
        finally
        {
            if (crStream != null)
            {
                crStream.Flush(); crStream.Close();
            }
            if (stream != null)
            {
                /*stream.Flush(); */
                stream.Close();
            }
        }
        #endregion
    }

0 个答案:

没有答案