通过查询字符串传输一个小的XML序列化对象

时间:2014-03-28 18:28:58

标签: c# xml

我有一个包含三个左右数据项的小对象,我想序列化并在查询字符串中发送,以便对查询字符串进行模糊处理。该页面是从桌面应用程序中的Web浏览器控件打开的,因此使用查询字符串似乎是最佳选择。

我可以使用System.Web.HttpUtility.UrlEncode方法,但我怀疑这对用户来说太明显了。

如何对XML序列化对象进行编码,使其显示为一组没有非法字符的字母和数字?

e.g。 <foo><bar>url_goes_here</bar></foo>变为lskjfgdf98gsjkhgdf786sg987dfkjlnadfg89

1 个答案:

答案 0 :(得分:1)

Encode功能在VB.NET桌面应用程序中:

Dim plaintext as String = myState.Serialize
Dim bytes As Byte() = ASCIIEncoding.UTF8.GetBytes(plaintext)
Dim encrypted As Byte() = Encryption.Encrypt(bytes, key)
Dim encodedtext As String = Convert.ToBase64String(encrypted)
Return System.Web.HttpUtility.UrlEncode(encodedtext)

解码函数位于C#ASP.NET网站中:

byte[] decodedtext = Convert.FromBase64String(querystring);
byte[] decrypted = Encryption.Decrypt(decodedtext, key);
string plaintext = ASCIIEncoding.UTF8.GetString(decrypted);
State mystate = new State();
mystate.Deserialize(plaintext);
return mystate;