So, I have some data in the form of:
<foo><bar>test</bar></foo>
What .NET classes/f
Using the System.Xml.XmlDocument
class...
Dim Val As String = "<foo><bar>test</bar></foo>"
Dim Xml As String = HttpUtility.HtmlDecode(Val)
Dim Doc As New XmlDocument()
Doc.LoadXml(Xml)
Dim Writer As New StringWriter()
Doc.Save(Writer)
Console.Write(Writer.ToString())
Use System.Net.WebUtility.HtmlDecode since .NET 4.0 if pretty printing is not important.
Here's one that I use, pass in an Xml string, set ToXml
to true if you want to convert a string containing "<foo/><bar/>" to the native xml equivalent, "#lt;foo/#gt;#lt;bar#gt;" - replace the hash with the ampersand as this editor keeps escaping it...likewise, if ToXml
is false, it will convert a string containing the "#lt;foo/#gt;#lt;bar#gt;" (replace the hash with the ampersand)to "<foo/><bar/>"
string XmlConvert(string sXml, bool ToXml){ string sConvertd = string.Empty; if (ToXml){ sConvertd = sXml.Replace("<", "#lt;").Replace(">", "#gt;").Replace("&", "#amp;"); }else{ sConvertd = sXml.Replace("#lt;", "<").Replace("#gt;", ">").Replace("#amp;", "&"); } return sConvertd; }
(replace the hash with the ampersand as this editor keeps escaping it within the pre tags)
Edit: Thanks to technophile for pointing out the obvious, but that is designed to cover only the XML tags. That's the gist of the function, which can be easily extended to cover other XML tags and feel free to add more that I may have missed out! Cheers! :)
you can use this code.
string p = "<foo><bar>test</bar></foo>";
Console.WriteLine(System.Web.HttpUtility.HtmlDecode(p));