14

How does one post XML in Stack Overflow?

I've tried using <pre> and backticks to no avail. I suppose that I could go through and try indent every line by four spaces but that would get really tedious really fast.

Is there any way to easily post XML in a question?

Edit:

URL of question: Configuring ELMAH: Unrecognized config section error

I tried putting the XML inside <code> tags and that sliced and diced the XML in a very odd fashion.

  • Why not post to where you are having the issue? – GEOCHET Aug 26 '09 at 20:43
10

Why not just use the code block?

  • There is a code button in the editor toolbar:
  • You could highlight the XML and press ctrl k
  • You could indent the XML with 4 spaces.

Edit:

In your case, you are getting close to exceeding the maximum length of a post. You should be more terse or host it externally.


Edit:

I fixed your post for you by highlighting the XML and clicking the button like I said.


More information:

https://meta.stackoverflow.com/editing-help

  • Or Ctrl-K, or use a real text editor to do the indent, or the toolbar button... – Joel Coehoorn Aug 26 '09 at 20:44
  • Sorry about that; I thought I had solved it by putting the XML inside a code block only to realize upon re-reading the post that much of the XML had been sliced off. – Zian Choy Aug 26 '09 at 20:49
  • If the length has been exceeded, why does it remove a piece in the middle and not cut off the end? – Brandon Aug 26 '09 at 20:53
  • See edits. OP did not read my post apparently. – GEOCHET Aug 26 '09 at 20:53
  • 1
    Thats odd, I tried adding 4 spaces, and all it did was move the line numbers over, the XML code still didn't show up in the preview. – Brandon Aug 26 '09 at 20:55
  • I've read your post several times but I probably missed one of your edit blurbs. Sorry about that. It looks like the XML part is back at last. Thank you so much. – Zian Choy Aug 26 '09 at 20:59
  • @Zian: You clearly did not read it. All I did was highlight your stuff and click the code button. – GEOCHET Aug 26 '09 at 21:03
  • @Rich B I thought the code block button simply inserted <code> tags. Until I dived into the source code of the post (so to speak), I didn't realize that the code block button inserted spaces. – Zian Choy Aug 26 '09 at 21:07
  • Note that in case the XML does not get recognized despite the code block, one can use <!-- language: lang-xml -->. – eis Jan 13 '14 at 12:54
8

Another potential cause of XML not being displayed correctly is if the code is part of a list; either numerated #) or bulleted -. In this case you need 8 spaces in front of the XML code for it to be escaped instead of the standard 4 spaces.

  • For example, unescaped XML:

    Not escaped

  • Example, correctly escaped:

    <style>Correctly escaped</style>
    

As described in this post: Post XML in answer

1

Expanding on this, for lists, there's also a second way:

  • If you have a list, you can use <!-- --> to break the formatting

    This is with 4 spaces

This is with 4 spaces and an invisible HTML comment

Otherwise, lists requires 4 + 4 * indentLevel spaces to format properly. It's only critical for HTML, XML, and other tag-based languages, as these are invisible and become post formatting rather than post content. The code formatting of 4 spaces is relative to the current level of indentation. A list has a standard indentation of 4, a list child item has a standard indentation of 8, and so on.

Generally though, CTRL+K or clicking the two brackets on the toolbar is enough. Or, of course, manually indenting everything with 4 (or more) spaces). Unfortunately, CTRL+K/bracket button doesn't work properly in lists. It just changes it between 0 and 4 spaces, like it's any other code block.


With the new code-block formatting using three backticks, it's now possible to format without a comment or additional spaces. Example:

* List item

    ```
    Nested code
    ```

```
Separate code
```

  • List item

    Nested code
    
Separate code
-2

Depending on how much you are trying to post, using an outside repository like PasteBin may be your most efficient method.

  • 2
    Or he could just use a code block on SO.com and not make external links for no reason. – GEOCHET Aug 26 '09 at 20:47
  • 1
    Or when the post is too big he could use PasteBin :) – TheTXI Aug 26 '09 at 20:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .