开发者

Write Serialized XML to String and Display in Browser

开发者 https://www.devze.com 2023-01-09 13:16 出处:网络
I have a serialized object which needs to be sent as an encrypted XML string. I am able to save the serialized object to a well-formed XML file, but this is not what I want. I have already got Rijndae

I have a serialized object which needs to be sent as an encrypted XML string. I am able to save the serialized object to a well-formed XML file, but this is not what I want. I have already got Rijndael encrypt/decrypt working for a sample string.

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);

I want to be able to display the XML string in the browser, before encryption, to test that the correct encrypted string is being sent to the decrypt method on the other machine.

I have been beating on this for a week and have looked to other answers on SO, such as: How to return XML in ASP.NET?

Can anyone show me the correct syntax to display the generated XML as a string in the browser?

[UPDATE开发者_如何学C] Here is what I'm trying to render the 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);

[UPDATE 2]

If I remove the "text/xml" content type, I get the following when I view source (is this correct?):

<?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>

[Update 3]

Working version:

            #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


it is quite likely that you need to set the ContentType on your Response object. This corresponds to the MIME type. for xml it should be text/xml


HtmlEncoding the XML string will also resolve the issue.

Response.Write(HttpUtility.HtmlEncode(stringData ));

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号