I have xml data that I am returning to my view. I am putting it in a textarea, but this displays it unformatted. How can I format the xml for display in my view?
I will be displaying the xml in only part of the page, so I can't let IE display it. I want it to be in standard xml indented format.
Go to http://www.xmlviewer.org/ in your computer's web browser. This viewer allows you to upload an XML file to view its code, as well as choose different viewing formats.
XML files are encoded in plaintext, so you can open them in any text editor and be able to clearly read it. Right-click the XML file and select "Open With." This will display a list of programs to open the file in. Select "Notepad" (Windows) or "TextEdit" (Mac).
To bind an XML data to an HTML table, add the datasrc (data source) attribute to the table element, then add the datafld (data field) attributes to any other inline eg. <div>, <span> or <input> tags between the <td> elements.
Use & in place of & . Just in case anyone reaches this question the same i did, there is the list of escape characters in . xml files and how to escape them: ibm.com/support/knowledgecenter/en/SSEQTP_liberty/…
If, by "formatting", you mean indented then you can load it into XmlDocument and write it into an XmlWriter initialized with an XmlWriterSettings that has Indent set to true
.
private string FormatXml(string input)
{
XmlDocument doc = new XmlDocument();
doc.LoadXml(input);
using (StringWriter buffer = new StringWriter())
{
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
using (XmlWriter writer = XmlWriter.Create(buffer, settings))
{
doc.WriteTo( writer );
writer.Flush();
}
buffer.Flush();
return buffer.ToString();
}
}
Simply load the XML into an XElement
, then use XElement.ToString()
.
You can use an XSLT to convert your XML into XHTML and then display that.
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