How can I decode HTML characters in C#?

前端 未结 10 2029
夕颜
夕颜 2020-11-22 03:38

I have email addresses encoded with HTML character entities. Is there anything in .NET that can convert them to plain strings?

相关标签:
10条回答
  • 2020-11-22 03:54

    For strings containing   I've had to double-decode the string. First decode would turn it into the second pass would correctly decode it to the expected character.

    0 讨论(0)
  • 2020-11-22 04:00

    On .Net 4.0:

    System.Net.WebUtility.HtmlDecode()
    

    No need to include assembly for a C# project

    0 讨论(0)
  • 2020-11-22 04:02

    It is also worth mentioning that if you're using HtmlAgilityPack like I was, you should use HtmlAgilityPack.HtmlEntity.DeEntitize(). It takes a string and returns a string.

    0 讨论(0)
  • 2020-11-22 04:05

    As @CQ says, you need to use HttpUtility.HtmlDecode, but it's not available in a non-ASP .NET project by default.

    For a non-ASP .NET application, you need to add a reference to System.Web.dll. Right-click your project in Solution Explorer, select "Add Reference", then browse the list for System.Web.dll.

    Now that the reference is added, you should be able to access the method using the fully-qualified name System.Web.HttpUtility.HtmlDecode or insert a using statement for System.Web to make things easier.

    0 讨论(0)
提交回复
热议问题