Question

Is there any way to turn off auto-formatting for arbitrary regions in Visual Studio?

I have automatic formatting set to indent exactly as I like. However, for a specific region (in particular, one having to do with creating an XML document), I'd like the indentation to make clear the structure of the XML document being created rather than the C# that creates it. For example:

// Format this normally.
public void WriteXMLDocument() {
    // ...
    using (XmlWriter x = XmlWriter.Create(filename)) {

    // Create XML document
    #region dont-format-this     // <-- Any way to get VS to recognize something like this?
        x.WriteStartDocument();
        x.WriteStartElement("RootElement");
            x.WriteStartElement("ChildElement1");
                x.WriteStartElement("GrandchildElement1a");
                    x.WriteElementString("GreatGrandchildElement1a1");
                    x.WriteElementString("GreatGrandchildElement1a2");
                x.WriteEndElement();
                x.WriteElementString("GrandchildElement1b");
            x.WriteEndElement();

            x.WriteStartElement("ChildElement2");
            x.WriteEndElement();
        x.WriteEndElement();
        x.WriteEndDocument();
    #endregion

    }
}

Obviously I don't expect Visual Studio to guess how to format what I'm doing, but I'd rather it just not try within this region. Otherwise every time I edit a line, VS gets ambitious and tries to undo my work.

Was it helpful?

Solution 3

I have verified the following for Visual Studio 2019 Professional 16.11.5.

#pragma warning disable IDE0055
// Discard formatting in this region
#pragma warning restore IDE0055

OTHER TIPS

As far as I know there is no feature to have different behavior within the same file in Visual Studio and there is only a feature request for Resharper to add exactly this. But that hasn't been addressed yet afaik and Resharper wasn't mentioned to be available.
I would have another poor man's solution which also supports the intention that you are trying to follow to bundle statements of the same depth in style. You can add {}-brackets to enclose things that are logically inside of what you are doing. So the formating would stay.

Potential drawback (if you see it this way) is You have more lines because of the brackets:

    using (XmlWriter x = XmlWriter.Create(filename)) {
            // Create XML document

            #region dont-format-this     // <-- Any way to get VS to recognize something like this?

            x.WriteStartDocument();
            {
                x.WriteStartElement("RootElement");
                {
                    x.WriteStartElement("ChildElement1");
                    {
                        x.WriteStartElement("GrandchildElement1a");
                        {
                            x.WriteElementString("GreatGrandchildElement1a1");
                            x.WriteElementString("GreatGrandchildElement1a2");
                        }
                        x.WriteEndElement();
                        x.WriteElementString("GrandchildElement1b");
                    }
                    x.WriteEndElement();
                }

                x.WriteStartElement("ChildElement2");
                x.WriteEndElement();
            }
            x.WriteEndElement();
            x.WriteEndDocument();
        }
        #endregion

    }

This only addresses the OP Question to keep the indentation of the example problem. It is not disabling parts of the file as what is @Noseratio asking.

As @Uwe has already stated there is no way in VS to do that. This is being offered only as a work around, not an answer to the original question. This will maintain formatting and let you visually see the xml structure as you want.

var settings = new XmlWriterSettings();
settings.Indent = true;
settings.OmitXmlDeclaration = true;
settings.ConformanceLevel = ConformanceLevel.Auto;

using (XmlWriter x = XmlWriter.Create(filename, settings))
{
    var xml = @"
        <RootElement>
            <ChildElement1>
                <GrandchildElement1a>
                    <GreatGrandchildElement1a1></GreatGrandchildElement1a1>
                    <GreatGrandchildElement1a2></GreatGrandchildElement1a2>
                </GrandchildElement1a>
            </ChildElement1>

            <ChildElement2>
            </ChildElement2>
        </RootElement>
    ";

    XDocument doc = XDocument.Parse(xml);

    x.WriteRaw(doc.ToString());
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top