views:

118

answers:

1

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: http://stackoverflow.com/questions/543319/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] 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"&gt;
  <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
+1  A: 

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

Muad'Dib
When I added the Content Type, I got: "XML Parsing Error: junk after document element"
IrishChieftain
sounds like malformed xml. I would put a breakpoint at your Response.Write and look at the stringData. Make sure it has a leading <xml> tag
Muad'Dib
I can see what appears to be correct XML, but only when I remove the Content Type? See Update 2 above.
IrishChieftain
Your XML looks right. What browser are you viewing it in? chrome will only display the text, not the tags in a "tree view" manner.
Muad'Dib
Firefox 3.6, IE9 and Chrome all displaying only the text, which is what confused me but I'm still curious why the content type gives that error "junk" message?
IrishChieftain
yea, that IS bizarre. could be unprintable characters in the string. could be other parts of the header disagree with your ContentType
Muad'Dib
I changed order of execution in my code and IE9 and Firefox now display the XML when Content Type is set to "text\xml" :)
IrishChieftain
I was going to say that you need to change headers *before* a `Flush`.
Steven Sudit