使用 XML 字符串序列化 XML
我必须生成以下 XML,
<object>
<stuff>
<body>
<random>This could be any rondom piece of unknown xml</random>
</body>
</stuff>
</object>
我已将其映射到一个类,其主体属性为字符串类型。
如果我将正文设置为字符串值:“
的任何随机片段”,
该字符串会在序列化过程中进行编码。如何不对字符串进行编码,使其以原始 XML 形式写入?
我还希望能够反序列化它。
I have to produce the following XML
<object>
<stuff>
<body>
<random>This could be any rondom piece of unknown xml</random>
</body>
</stuff>
</object>
I have mapped this to a class, with a body property of type string.
If I set the body to the string value: "<random>This could be any rondom piece of unknown xml</random>
"
The string gets encoded during serialization. How can I not encode the string so that it gets written as raw XML?
I will also want to be able to deserialize this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
XmlSerializer
根本不信任您从字符串
生成有效的xml。如果您希望成员是临时 xml,则它必须类似于XmlElement
。例如:使用
Body
命名为random
的XmlElement
,InnerText
为“这可以是任何随机块未知的 xml"
可以工作。XmlSerializer
will simply not trust you to produce valid xml from astring
. If you want a member to be ad-hoc xml, it must be something likeXmlElement
. For example:with
Body
anXmlElement
namedrandom
withInnerText
of"This could be any rondom piece of unknown xml"
would work.