0

In my ASP.NET MVC 5 website i have the following scenario:

When user clicks on a button, it calls an JsonResult that returns a string representing a XML. but when i try to displey it on a div, i get just the node values of this string.

<div id="xmlContent"/>

<script language="javascript">
function CustomButtonClick(s, e) {
    var gridKey = s.GetRowKey(e.visibleIndex);
    if (e.buttonID === 'btnShowXmlContent') {

        $.getJSON('@Url.Action("GetXmlContent")', { gridKey: gridKey }, function (data)    { 

            alert(data);
            $('#xmlContent').html(data);
            pcXmlContent.Show();
        });
    };       
 }    
 </script>

The plain XML:

<?xml version="1.0" encoding="utf-16"?>   
 <HEADER>
    <NumeroOperacao>201406030927460355</NumeroOperacao>        
  </HEADER>
  <MESSAGE>
    <ERROR>Object reference not set to an instance of an object.</ERROR>
  </MESSAGE>

The HTML version of this xml:

 201406030927460355
 Object reference not set to an instance of an object.

I would like to know how can i display this xml as plain xml? Thanks

1 Answer 1

1

jQuery's html() function accepts HTML, since XML looks like HTML (and the browser doesn't mind) it will create HEADER and NumeroOperacao etc. as HTML nodes, which leaves just the textnodes visible.

Use text() to add the value as text.

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

2 Comments

Thanks, i tried and it almost work, but i get the XML unformated, i mean, i have the tags on the same line, without breaking lines per node like in the original XML file that i posted on the OP.
Add style="white-space: pre;" to the element to preserve whitespace.

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.