Working with large XML files without losing your sanity

Reason

Speaking as a Sitecore developer, XML files have the tendency to become so large that it’s tricky to figure out where exactly you’re working, seen from an XPath perspective. This makes the process of creating e.g. Sitecore include files tedious and error prone.

A while ago I created a small Visual Studio plugin which keeps track of the XPath matching your cursor position when working in files containing XML-markup (e.g. Web.config, NAnt files, MSBuild files, Solr configuration files, Spring.NET files … you get the point). I’ve recently released v3 of the plugin and thought it was about time to do a new writeup of the features.

The plugin can be installed either by downloading it from the Visual Studio Gallery (http://visualstudiogallery.msdn.microsoft.com/c06c7b10-41c3-4aa9-8707-570eb9d879e6) or by opening “Tools -> Extensions and Updates…” and searching for “XPath”.

Plugin installation

Code

The source code can be found at https://github.com/uli-weltersbach/XPathInformation.

Example

Statusbar

The XPath matching the current caret position is shown in the Visual Studio statusbar:

Screenshot - Statusbar

Copy XML structure

The basics for a Sitecore include file can be created easily with the “Copy XML structure” command. Simply select an element and choose “Copy XML structure” from the context menu:

Screenshot - XML Structure copy

Copy

Screenshot - XML Structure paste

Paste

Note that xmlns:patch="http://www.sitecore.net/xmlconfig/" is not added to the root automatically.

Copy XPath

It’s possible to copy the XPath at the current caret position in 3 different ways:

  • Generic – matches one or more elements
  • Absolute – matches the current element by index
  • Distinct – attempts to determine an XPath which only matches the current element by using distinct attribute values of the current node and its ancestors
Screenshot - Copy XPath

XPath variations available from the context menu.

Options

It’s possible to configure which attributes are copied to the clipboard from the XPath options page found in “Tools -> Options -> XPath Information”. Check the option descriptions for details.

Screenshot - Options

XPath Information options page

For personal use I’ve configured the attributes “id”, “name”, “type” and “method” to be used in distinct paths, as these make most sense (to me) when working with Sitecore. This way I can quickly copy the correct XPath to the clipboard when using e.g. “patch:before” in Sitecore include files.

My XPath settings for the "Copy distinct XPath" command

My XPath settings for the “Copy distinct XPath” command.

Example of the distinct XPath to a Sitecore pipeline processor

Example of the distinct XPath to a Sitecore pipeline processor when using the value of the “type” attribute, which is unique for this element among its siblings.

Please use https://github.com/uli-weltersbach/XPathInformation/issues for feature requests and bug reports.
If you find the plugin helpful please rate it on Visual Studio Gallery. 🙂

6 thoughts on “Working with large XML files without losing your sanity

  1. Pingback: Favorite workstation tools - Geekcore

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s