5

I have a string in a ASP.NET MVC details page with the value of

<this><is sample = "attribute"><xml><power>!!!</power></xml><oh><yeah></yeah></oh></is></this>.

I want it to display as follows:

<this>
  <is sample = "attribute">
    <xml>
       <power> !!! </power>
    </xml>
    <oh>
       <yeah>
       </yeah>
    <oh>
  </is>
</this>

Things I have tried:

1: How to Display Formatted XML - best answer and richards answer

2: xmlwriter.writeraw();

3: basic linq-to-xml (i'm not very good with this)

EDIT: I am displaying the string as follows and was wondering if this may have something to do with it:

<%: *formatted string goes here* %>
1
  • How did XElement.ToString not work? What was the output? Keep in mind you'll have to encode the output if you're displaying it in a web page (you can use Html.Encode()). Commented Feb 6, 2012 at 16:16

5 Answers 5

7

I was doing it in this way:

protected string FormatXml(XmlNode xmlNode)
{        
    StringBuilder builder = new StringBuilder();

    // We will use stringWriter to push the formated xml into our StringBuilder bob.
    using (StringWriter stringWriter = new StringWriter(builder))
    {
        // We will use the Formatting of our xmlTextWriter to provide our indentation.
        using (XmlTextWriter xmlTextWriter = new XmlTextWriter(stringWriter))
        {
            xmlTextWriter.Formatting = Formatting.Indented;
            xmlNode.WriteTo(xmlTextWriter);
        }
    }

    return builder.ToString();
}

http://forums.asp.net/t/1145533.aspx/1

Sign up to request clarification or add additional context in comments.

8 Comments

ok, the parameter is a node, so will i have to break down the xml into all it's nodes and have multiple strings? or can i put the entire xml into one node?
You can pass the RootNode. Also as I know you can pass XmlDocument too. It has WriteTo method as well.
ok, got it. i see the indents but now i need help with the new line which i know is in xmltextwriter settings.
Didn't get question. New line where?
right now it is displaying it as: <here is="some" more="oh"> <is some="data"> <test>xml info</test> </is> </here> without any new lines, but the indent is working (all is being displayed on one line atm). so right now it is being displayed like my first sample html in the question plus the indents, but i want it to display like the second sample html (the easily readable html)
|
3

The problem is you are outputting text, which will then be interpreted by the browser in the default way that text is handled - it doesnt know that it is XML.

What you need is a library to correctly format the text using standard XML rules.

You could try Google Prettify - which is a Javascript library to format code (it supports XML as well as many other programming languages). There is also a .NET based formatter that you could use, I think it was written by Stack Overflow and open sourced - but I cannot find it right now.

1 Comment

a very likely possibility. i create a builder StringBuilder that displays the correct indentation, but when i display it on my view page it is no longer correct.
1

All your problems are because of all browsers are truncating the spaces in xml.
Try to use &nbsp; to draw intends or simply add the declaration of the xml to start of the page:

<?xml version="1.0" ?>
<this>
  <is sample = "attribute">
    <xml>
       <power> !!! </power>
    </xml>
    <oh>
       <yeah>
       </yeah>
    <oh>
  </is>
</this>

All modern browsers will handle this correctly.

1 Comment

not sure if you can see who downvoted you, but you still have my upvote. sorry for the downvote:(
1

from https://stackoverflow.com/a/16605336/1874 , here is a simple 3 line solution:

var xml = "<root><A><B>0</B><C>0</C></A><D><E>0</E></D></root>";

XDocument doc = XDocument.Parse(xml);
Console.WriteLine (doc.ToString());

or inline as

XDocument.Parse(xmlstring).ToString()

place the result inside of (for example inspect the code blocks on this StackOverflow page):

<pre></pre>

Comments

0

try setting the content type to xml and add the xml header before your data, I use this simple prep function for my webservices, r is the Response object:

public void prepXml()
{
    r.AddHeader("Content-Type", "text/xml");
    r.Write("<?xml version=" + '"' + "1.0" + '"' + " encoding=" + '"' + "utf-8" + '"' + " ?>");
}

Comments

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.