Silverlight doesn't have the
XmlDocument
class. You'd otherwise be able to do it as Ian Randell does
here.
Instead, Silverlight currently offers the
XDocument
and
XmlWriter
classes. So you'd do it with these two classes as shown below.
Just call the
XmlFormat
method below and display the result, i.e., with indenting and line breaks, in your UI control.
using System.Xml;
using System.Xml.Linq;
using System.Text;
private string FormatXml(string stringXml)
{
StringReader stringReader = new StringReader(stringXml);
XDocument xDoc = XDocument.Load(stringReader);
var stringBuilder = new StringBuilder();
XmlWriter xmlWriter = null;
try
{
var settings = new XmlWriterSettings();
settings.Indent = true;
settings.ConformanceLevel = ConformanceLevel.Auto;
settings.IndentChars = " ";
settings.OmitXmlDeclaration = true;
xmlWriter = XmlWriter.Create(stringBuilder, settings);
xDoc.WriteTo(xmlWriter);
}
finally
{
if (xmlWriter != null)
xmlWriter.Close();
}
return stringBuilder.ToString();
}
License
This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)