How to decode string to XML string in C#

后端 未结 7 1506
挽巷
挽巷 2020-11-27 20:41

I have a string (from a CDATA element) that contains description of XML. I need to decode this string into a new string that displays the characters correctly using C#

相关标签:
7条回答
  • 2020-11-27 21:09

    You might also consider the static parse method from XDocument. I'm not sure how it compares to others mentioned here, but it seems to parse these strings well.

    Once you get the resulting XDocument, you could turn around with ToString to get the string back:

    string parsedString = XDocument.Parse("<My XML />").ToString();
    
    0 讨论(0)
  • 2020-11-27 21:13

    You can use HTML.Raw. That way the markup is not encoded.

    0 讨论(0)
  • 2020-11-27 21:14
    1. HttpUtility.HtmlDecode from System.Web
    2. WebUtility.HtmlDecode from System.Net
    0 讨论(0)
  • 2020-11-27 21:15

    You can use System.Net.WebUtility.HtmlDecode instead of HttpUtility.HtmlDecode

    Useful if you don't want System.Web reference and prefer System.Net instead.

    0 讨论(0)
  • 2020-11-27 21:20

    HttpUtility.HtmlDecode(xmlString) will solve this issue

    0 讨论(0)
  • 2020-11-27 21:29

    You just need to replace the scaped characters with their originals.

    string stringWanted= existingString.Replace("&lt;", "<")
                                                       .Replace("&amp;", "&")
                                                       .Replace("&gt;", ">")
                                                       .Replace("&quot;", "\"")
                                                       .Replace("&apos;", "'");
    
    0 讨论(0)
提交回复
热议问题