将序列化 XML 写入字符串并在浏览器中显示

发布于 2024-09-12 11:30:11 字数 2593 浏览 4 评论 0原文

我有一个序列化对象,需要将其作为加密的 XML 字符串发送。我能够将序列化对象保存到格式良好的 XML 文件中,但这不是我想要的。我已经让 Rijndael 加密/解密了示例字符串。

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 字符串,以测试是否将正确的加密字符串发送到另一台计算机上的解密方法。

我已经为此奋斗了一周,并查看了 SO 上的其他答案,例如: 如何在 ASP.NET 中返回 XML?

谁能告诉我在浏览器中将生成的 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]

如果删除“text/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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一紙繁鸢 2024-09-19 11:30:11

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

夜未央樱花落 2024-09-19 11:30:11

对 XML 字符串进行 HtmlEncoding 也可以解决该问题。

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

HtmlEncoding the XML string will also resolve the issue.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文