我有一个需要作为加密XML字符串发送的序列化对象。我能够将序列化的对象保存为格式良好的XML文件,但这不是我想要的。我已经有Rijndael加密/解密工作的样本字符串。将序列化XML写入字符串并在浏览器中显示
Person person = new Person("Irish", "Chieftain");
XmlSerializer xmlSerializer = new XmlSerializer(typeof(Person));
// Write serialized XML to file
System.Guid guid = System.Guid.NewGuid();
StreamWriter streamWriter
= new StreamWriter(@"C:\application" + "_" + guid.ToString() + ".xml")
xmlSerializer.Serialize(streamWriter.BaseStream, person);
我希望能够在浏览器显示XML字符串,在加密之前,要测试的正确加密的字符串被发送到解密方法的另一台机器上。
我一直在敲打这一个星期,有这么看着其他的答案上,如: How to return XML in ASP.NET?
谁能告诉我正确的语法,以显示生成的XML在浏览器中的字符串?
[更新] 这里是我试图解析XML:
MemoryStream memoryStream = new MemoryStream();
XmlTextWriter xmlWriter2 = new XmlTextWriter(memoryStream, Encoding.UTF8);
xmlWriter2.Formatting = Formatting.Indented;
xmlSerializer.Serialize(xmlWriter2, person);
memoryStream = (MemoryStream) xmlWriter2.BaseStream;
UTF8Encoding encoding2 = new UTF8Encoding();
stringData = encoding2.GetString(memoryStream.ToArray());
Response.ContentType = "text/xml";
Response.Write(stringData);
[更新2]
如果我删除了 “文本/ xml” 的内容类型,当我查看源代码时,我收到以下内容(这是否正确?):
<?xml version="1.0" encoding="utf-8"?>
<Person xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<FirstName>Irish</FirstName>
<SecondName>Chieftain</SecondName>
</Person>
[更新3]
工作版本:
#region Display original string
// Write serialized XML to a string - Display purposes.
MemoryStream memoryStream = new MemoryStream();
XmlTextWriter xmlWriter2
= new XmlTextWriter(memoryStream, Encoding.UTF8);
xmlWriter2.Formatting = Formatting.Indented;
xmlSerializer.Serialize(xmlWriter2, person);
memoryStream = (MemoryStream) xmlWriter2.BaseStream;
UTF8Encoding encoding2 = new UTF8Encoding();
stringData = encoding2.GetString(memoryStream.ToArray());
Response.Clear();
Response.ContentType = "text/xml";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetAllowResponseInBrowserHistory(true);
Response.Write(stringData);
Response.End();
#endregion
我把你的xml粘贴到一个文本文件中,将它从.txt改为.xml并在IE中打开它。没有问题,它显示树视图,一切都很好。我用chrome加载它,它显示的是文本“Irish Chiegtain” – 2010-08-01 02:42:50
感谢Muad'Dib,标记为答案:-) – IrishChieftain 2010-08-01 03:04:57
我在这里错过了一些东西:加密适合哪里?加密的XML不符合XML的要求... – 2010-08-01 03:35:48