Search code examples
c#visual-studiovisual-studio-2010visual-studio-2015formatting

Turn off auto-formatting for a #region in Visual Studio 201x


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.


Solution

  • 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