65.9K
CodeProject is changing. Read more.
Home

Reformatting XML

starIconstarIconstarIconstarIconemptyStarIcon

4.00/5 (1 vote)

Feb 10, 2001

viewsIcon

63645

downloadIcon

672

A Developer Studio macro to reformat XML

  • Download source - 2 Kb
  • So, it's a weekend, I'm hunting for a Developer Studio macro that will format XML, and I come across Alvaro Mendez's nice little C/C++ formatting macro (MakeCodeNicer). I spend a few hours whittling it down to what I need for XML and voila! I now have an XML formatter (MakeXMLNicer).

    If you do a lot of work with Microsoft's XML parser (particularly using it to write out XML), you've probably noticed you can't tell the parser to insert white space for new elements. So you end up with XML tags lined up in a nice long string. Makes it really hard to follow the XML structure.

    The macro will handle the main <?xml?> tag, comment tags, normal start/end element tags and the special single element tags (<foo/>). Indention is handled by inserting tabs although you could change this pretty easily to spaces if you prefer. Any existing indention or new line breaks are removed.

    Attribute values are dealt with in a special way. Any embedded new lines in the value are stripped except when the attribute value exceeds 1000 characters - DevStudio can't handle very long lines and I've run into XML with very long base64-encoded attribute values. In these cases, the attribute values are left alone.

    Element text (the text between a start tag and end tag) is completely left alone - no tabs, new lines, etc are removed since it's impossible to know if these might be important to the context of the element. Some XML might use tabs in element values for a special meaning - stripping them would damage the XML.

    BTW, a thanks goes to Alvaro for the inspiration and some macro code to save me some time.

    Enjoy.

    Fixes:

    • 3 Apr 2001: Fixed problem w/ removing spaces in XML element values.

    Known Problems:

    • Element (tag) names that are "broken" by new lines are not handled. These would show up as names with a single embedded space to replace the new line. This would "break" the XML, but should be a very rare occurrence.
    • The nasty DevStudio "Paste" bug (where Paste stops working) can occur with macros that use the ActiveDocument.Selection method to assign replacement values. I minimized use of this method as much as possible to reduce the likelihood you will encounter this problem, but it's still there. Unfortunately, Joshua Jensen's excellent PasteFix add-in won't solve this problem since ActiveDocument.Selection is using clipboard functions directly and not going thru the Ctrl-V keyboard shortcut. But I'd heartily recommend that you get Joshua's add-in to eliminate the problem everywhere else.
    • The macro will reformat the entire file - there is no support for reformatting a selection yet.