我有一本字典,我把它转成 XML,然后用 SHA1 散列。
string xmlMessageCode = inputDictionary.ToXML(); //Extension method.
UnicodeEncoding UE = new UnicodeEncoding();
SHA1Managed hasher = SHA1Managed();
byte[] hashString = Encoding.UTF8.GetBytes(xmlMessageCode.ToCharArray());
byte[] hashCode = hasher.ComputeHash(hashString);
string computedHashString = UTF8Encoding.UTF8.GetString(hashCode);
return computedHashString;
之后,我将值放入对象属性中,然后将这些对象的集合序列化为 XML:
XmlSerializer ser = new XmlSerializer(typeof(T));
XmlWriterSettings settings = new XmlWriterSettings()
{
Indent = false,
OmitXmlDecleration = false,
Encoding = Encoding.UTF8
};
using(StringWriter sr = new StringWriter)
{
using(XmlWriter xmlr = XmlWriter.Create(sr, settings))
{
ser.Serialize(sr, newList);
}
return sr.ToString();
}
这会生成 XML,但是当我尝试验证生成的 XML 时,我在从散列字符串创建的属性中得到一个错误。
解决此问题的最佳方法是什么?我应该去掉无效字符还是有更优雅的解决方案?