So, I have some data in the form of:
<foo><bar>test</bar></foo>
What .NET classes/functions would I want to use to convert this to something pretty and write it out to a file looking something like this:
<foo>
<bar>
test
</bar>
</foo>
Be specific on the functions and classes please, not just "use System.XML". There seems to be a lot of different ways to do things in .NET using XML :(
Thanks
With as popular as XML is, when creating XML files you need to be able to escape certain characters that will not parse correctly if they are not escaped. Until recently I always did this like most other .Net programmers, I wrote a function to do it.
These special characters are also referred to as XML Metacharacters. By the process of escaping, we would be replacing these characters with alternate strings to give the literal result of special characters.
The reason for unescaping first is the content we were receiving contained unescaped as well as escaped special characters in the same XML element value, and we just hacked our way around using the said sequence of String.Replace commands to prevent use of more complex regex patterns.
If you don't mind 3rd party code and want to ensure no illegal characters make it into your XML, I would recommend Michael Kropat's answer. & isn't valid XML.
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())
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With