I'm trying to unescape XML entities in a string in .NET (C#), but I don't seem to get it to work correctly.
For example, if I have the string AT&T
, it should be translated to AT&T
.
One way is to use HttpUtility.HtmlDecode(), but that's for HTML.
So I have two questions about this:
Is it safe to use HttpUtility.HtmlDecode() for decoding XML entities?
How do I use XmlReader (or something similar) to do this? I have tried the following, but that always returns an empty string:
static string ReplaceEscapes(string text) { StringReader reader = new StringReader(text); XmlReaderSettings settings = new XmlReaderSettings(); settings.ConformanceLevel = ConformanceLevel.Fragment; using (XmlReader xmlReader = XmlReader.Create(reader, settings)) { return xmlReader.ReadString(); } }
xmlReader.MoveToContent()
did the trick, and that's exactly the solution I was looking for. I didn't really want to use HttpUtility because of the differences between HTML and XML, so your response was extremely helpful. – Angelikaangelina