我有一個需要作爲加密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