简体   繁体   中英

C# XML Serialization, Ignore Certain Nodes

Currently using the System.Xml.Serialization serializer. Deserializing a string that contains HTML markup will cause the serializer to remove the markup. What is the best solution to avoid this?

Here's what the XML looks like,

<String name="Computer3" lang="US_EN">
    HELLO <i>FRUITS</i>. I HAVE RELEASED A NEW VIDEO.
</String>

The <i> tags are getting removed upon deserialization, so the output message would look something like,

HELLO . I HAVE RELEASED A NEW VIDEO.

You've got to encode the string value to preserve any HTML markup in XML.

var encodeString = HttpUtility.HtmlEncode("HELLO <i>FRUITS</i>. I HAVE RELEASED A NEW VIDEO.");

Then you'll have to decode it whenever you're retrieving the value.

var normalString = HttpUtility.HtmlDecode("HELLO &lt;i&gt;FRUITS&lt;/i&gt;. I HAVE RELEASED A NEW VIDEO.");

Source [1]: https://msdn.microsoft.com/en-us/library/73z22y6h(v=vs.110).aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM