This project has moved. For the latest updates, please go here.

Output of code tag is not left-aligned

Topics: Developer Forum, User Forum
Jan 18, 2011 at 2:12 PM

I am using the code tag to embed sample code in my chm documentation, like below:

   <code lang="cs" source="..\..\xxx.cs" region="yyy"/>

The first line of the sample code in the output has an indent ,it is not left-aligned.

                    private xxx(zz z)
{ if (xx== null) return null;

Is there a solution to this? Any reply is appreciated,


Jan 18, 2011 at 9:06 PM

As I recall, <code> elements are only really valid if nested within an <example> element which is a top level element like <summary> or <remarks>.  Nesting <code> elements within a <para> element may have unintended side effects especially if that is within some other element such as <summary> or <remarks>.  A code block within a CDATA section of an XML fragment can cause the issue too.  Making sure the first line of code appears on a line by itself fixes that issue.  However, since it looks like you are importing from a source code file that may not be the case.  Leading spaces are okay, just make sure they are consistent within the block since that determines the amount trimmed off the front of each line.  If you've got some extra tab characters on the first line or a mix of spaces and tabs on the other lines, that may be causing the issue too since it would convert them to spaces and trim off the minimum amount from all the other lines but may leave excess space on the first line due to the extra converted tabs.



Mar 16, 2011 at 7:31 PM

The same problem occurs for me when using <para> and <code> in an overview topic.  Even flushing the first line all the way to the left (with or without CDATA) causes this rendering anomaly.  In the example below, the <?xml> tag renders with a much larger indent than the rest of the code.


<code language="xml">
<?xml version="1.0" encoding="utf-8"?>
            <eventLog <!-- ommitted --> />
            <eventsFile <!-- ommitted -->/>
            <!-- Optional overrides for specific Event Sources -->
            <eventSource id="My Event Source"
                      eventsFileLevel="Off" />
            <eventSource id="Webstore"
                      eventsFileLevel="Off" />

Mar 16, 2011 at 7:43 PM

Having the IndentHtml project property set to True can cause this issue in both XML comments and MAML topics.  Make sure it's set to false.



Mar 16, 2011 at 8:21 PM

Perfect answer! 

+1 would ask again.