If you have unformatted XML in a string and need to format it for visual presentation, e.g., for sending it to your UI text display control, here's how to do it in Silverlight 4.0.
Silverlight doesn't have the
XmlDocument class. You'd otherwise be able to do it as Ian Randell does in his article.
Instead, Silverlight currently (Jan 2011) offers the
XmlWriter classes. So you'd do it with these two classes as shown below:
Using the code
Just call the
XmlFormat method below and display the result, i.e., with indenting and line breaks, in your UI control.
private string FormatXml(string stringXml)
StringReader stringReader = new StringReader(stringXml);
XDocument xDoc = XDocument.Load(stringReader);
var stringBuilder = new StringBuilder();
XmlWriter xmlWriter = null;
var settings = new XmlWriterSettings();
settings.Indent = true;
settings.ConformanceLevel = ConformanceLevel.Auto;
settings.IndentChars = " ";
settings.OmitXmlDeclaration = true;
xmlWriter = XmlWriter.Create(stringBuilder, settings); xDoc.WriteTo(xmlWriter);
if (xmlWriter != null)