public static void OnXmlDocChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var browserControl = d as XmlBrowserControl;
if (browserControl == null) return;
var xmlString = e.NewValue as string;
try
{
var xmlDocument = new XmlDocument();
var xmlDocStyled = new StringBuilder(2500);
// mark of web - to enable IE to force webpages to run in the security zone of the location the page was saved from
// http://msdn.microsoft.com/en-us/library/ms537628(v=vs.85).aspx
xmlDocStyled.Append("<!-- saved from url=(0014)about:internet -->");
var xslt = new XslCompiledTransform();
//TODO: Do not forget to change the namespace, if you move the xsl sheet to your application
var resourceName = typeof (XmlBrowserControl).Assembly.GetManifestResourceNames()
.FirstOrDefault(r => r.ToLower().Contains("xml-pretty-print.xsl"));
var xsltFileStream =
typeof(XmlBrowserControl).Assembly.GetManifestResourceStream(
resourceName);
if (xsltFileStream != null)
{
//Load the xsltFile
var xmlReader = XmlReader.Create(xsltFileStream);
xslt.Load(xmlReader);
var settings = new XmlWriterSettings();
// writer for transformation
var writer = XmlWriter.Create(xmlDocStyled, settings);
if (xmlString != null) xmlDocument.LoadXml(xmlString);
xslt.Transform(xmlDocument, writer);
}
//browserControl.EditText.Text = xmlString;
browserControl.WebBrowser.NavigateToString(xmlDocStyled.ToString());
//browserControl.EditButton.Visibility = System.Windows.Visibility.Visible;
browserControl.CopyClipButton.Visibility = System.Windows.Visibility.Visible;
}
catch (Exception ex)
{
browserControl.WebBrowser.NavigateToString("Unable to parse xml. Correct the following errors: " + ex.Message);
}
}