0

我有这段代码来保留一些配置数据

XmlSerializer xmlSerializer = new XmlSerializer(typeof(Entities.Application));
TextReader textReader = new StreamReader(XMLFile);
sequences = (Entities.Application)xmlSerializer.Deserialize(textReader);
textReader.Close();

现在我想把它隐藏一点,并以某种方式在 XML 上使用二进制格式(无论如何都要保留 XML)。

有可能吗?(或者还有其他一些方法?)

怎么做?

谢谢!

4

2 回答 2

1

如果您想隐藏 XML,那么两个简单的选项是使用 Base64 编码 XML 或在 XML 元素中使用 Base64。

// Simple Base64 conversion (using UTF8 for simplicity)
// ========

// Assume [sequences] is variable of type [Entities.Application]
string xmlString;
var xs = new XmlSerializer(typeof(Entities.Application));
using (var sw = new StringWriter())
{
    xs.Serialize(sw, sequences);
    xmlString = sw.ToString();
}
string encoded = System.Convert.ToBase64String(
                    System.Text.Encoding.UTF8.GetBytes(xmlString));

// Converting encoded [Entities.Application] to decoded XML string,
// using some of your code for consistency.
string encoded;
using (TextReader textReader = new StreamReader(XMLFile))
{
    encoded = textReader.ReadToEnd();
    textReader.Close();
}
string decoded = System.Text.Encoding.UTF8.GetString(
                    System.Convert.FromBase64String(encoded));
XmlSerializer xmlSerializer = new XmlSerializer(typeof(Entities.Application));
using (var sr = new StringReader(decoded))
{
    sequences = (Entities.Application)xmlSerializer.Deserialize(sr);
}


// Inserting Base64 in XmlElement
// ========

// Optional: the old MSXML.DOMDocument had nodeTypedValue, and you
// can set the same attributes if you are going to use DOMDocument, although
// it is still a good idea to tag the element so you know its datatype.
node.SetAttribute("xmlns:dt", "urn:schemas-microsoft-com:datatypes");
node.SetAttribute("dt", "urn:schemas-microsoft-com:datatypes", "bin.base64");

// Assume serialized data has already been encoded as shown above
var elem = node.AppendChild(xmlDoc.CreateTextNode(encoded));
于 2012-08-01T16:37:25.677 回答
1

您仍然需要将二进制数据编码为文本。是不是已经压缩了?如果是这样,那可能是第一步。然后您需要决定如何对二进制文件进行编码。Base91 的效率很高:http: //base91.sourceforge.net/

于 2012-08-01T14:27:33.557 回答