I was working on a page where I had to display raw XML to an asp.net page and surprisingly it turned out to be quite challenging. Seriously, how hard can it be. I remember displaying xml by encoding it but that's not really an option in my scenario.
XML control – did not work
I tried using the asp.net XML control but it did not work. The control renders xml without displaying the raw xml. I could see the xml displayed in the source code of the browser but it would not appear in the browser.
Label & Literal control – did not work
Then I tried using the Label and Literal control but it did not display any content from the xml. The xml appeared in the source code but the browser could not interpret it. However, the controls can display xml once the xml is encoded.
<Pre> , <code>html elements – did not work
Then I tried using the <pre> and <code> elements but there was no improvement. The browser still would not display the xml. The <pre> tag basically can display xml once it is encoded.
TextArea element – finally it worked
Finally, I used the textarea element and it worked. The problem with textarea is that it is editable and it has other display features like border and resize options. Fortunately, these properties can be modified using css. So, here is the code to display xml in a textarea. I have used dummy xml for the snippet.
And here is the back end code to get the xml.
Now run the page and it should just work. With the height and width of the text area, set to something that is reasonable in your application.