Meta Stack Exchange is where users like you discuss bugs, features, and support issues that affect the software powering all 158 Stack Exchange communities.

What is meta?
Here's how it works:
  1. Any Stack Exchange user can ask a question
  2. The community provides support, votes on ideas, and reports bugs
  3. Your voice helps shape the way Stack Exchange operates

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?


URL of question:

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

share|improve this question
Why not post to where you are having the issue? – GEOCHET Aug 26 '09 at 20:43
up vote 7 down vote accepted

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.


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


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

More information:

share|improve this answer
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
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

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

share|improve this answer

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

share|improve this answer
Or he could just use a code block on and not make external links for no reason. – GEOCHET Aug 26 '09 at 20:47
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 .