1

I have a field within a database that stores some XML data. I display this data onto a page within an application that I am creating. I currently display the data on a jquery toggle like so...

<a  data-toggle="collapse" data-target="#demo">
    @Html.DisplayNameFor(model => model.AllXml)
</a>
<div id="demo" class="collapse">
    <div class="display-field">
        <p> @Html.DisplayFor(model => model.AllXml)</p>
</div>

The problem I am facing is that when the data is displayed on the page, it is not readable at all, as you would expect. I am wanting to make the XML data that is displayed on the page more readable. I have had a look at both Beautify and TreeView but I have no idea how this could be implemented to my solution. Could somebody please point me in the right direction?

Thanks in advance

1
  • There are a lot of online editors, you can embed in your page in readonly mode, in order to have a pretty xml, f.e. ace. Commented Aug 19, 2015 at 11:00

2 Answers 2

1

You can use the pre tag to preserve formatting strings.

<pre>@Html.DisplayFor(model => model.AllXml)</pre>
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, this is a quick fix to my problem. I would like to implement a TreeView if possible though, but this is helpful cheers
0

You can use LINQ to get

 try
 {
     XDocument doc = XDocument.Parse(xml);
     return doc.ToString();
 }
 catch (Exception)
 {
     return xml;
 }

You can use this to get an well formatted xml string that you can use in your html instead of just outputting the xml as is. Please also not that if you are using an old version of .NET you wont have access to LINQ[ .NET < V3.5]

3 Comments

Thanks for your input, where in my application would this go please?
if AllXml is a string, you can use the above code and put it in a method that would return string, then set model.AllXml = YourMethod(model.AllXml); this code would be in the controller Action.
you can also use @Html.Raw(model.Allxml) should the browser apply stying to your xml

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.