XML Document Transform (XDT) in VS 2010 (incl ‘.sitemap’ files)

16 08 2010

NOTE: The original blog on this can be found here.

Visual Studio 2010 has an added feature for web developers and this feature is called ‘Config Transform’. This enables web developers to modify the project’s Web.config file in an automated fashion during deployment of your project to various server environment (for more information on this go here).

Thanks to Vishal, we can now implement this XML Document Transformation technology into our own projects for any XML file. However, this will require a little bit of extra work from our side as this technology must be added for XML files other than the Web.config.
The reason for this document is not because I wanted to change normal XML files, but because of changes I wanted to make to the Web.sitemap XML file.

IMPORTANT: Keep backups of all the files you want to make changes to. Especially the “XmlDocumentTransform.targets” & “Microsoft.WebApplication.targets” files.
Here are the steps:

1. Download the XmlDocumentTransform.targets file here and save it to the following directory:

%ProgramFiles%\MSBuild\Microsoft\VisualStudio\v10.0\Web\XmlDocumentTransform.targets

For Web.sitemap changes to work, edit the above file on line 100 like the following (or just leave it if you focus on normal XML files):

FROM

Settings.xml;app.Config

TO

Web.sitemap;app.Config

XmlDocumentTransform.targets Edit..
Figure 1: XmlDocumentTransform.targets Edit..

Don’t worry about the Settings.xml you are replacing, in fact you can add any XML file to your project without further editing the “XmlDocumentTransform.targets” file and the XML Document transformation will still work for the extra added XML file(s).

2. In the ”Microsoft.WebApplication.targets” file located at: %ProgramFiles%\MSBuild\Microsoft\VisualStudio\v10.0\WebApplications…

Add the following line of code just before the tag:

Microsoft.WebApplication.targets Edit..
Figure 2: Microsoft.WebApplication.targets Edit..

NOTE: Changing the above file (Microsoft.WebApplication.targets) will allow you to use this technology with all Web Applications Projects on your machine. If you want this only for the current project put the above node into the .csproj or .vsproj as well. You can even check this file into Source Control so that the whole team can use it.

3. Open your .csproj or .vsproj file by right-clicking on your project (in the solution explorer) and selecting Unload Project. Then right-click again on the project and select “Edit ProjectName.csproj/.vsproj”. (A shorter version of this is by just right-clinking on the project and selecting “Edit Project File” like below.)

Edit Project File..
Figure 3: Edit Project File..

Now that the your project file is open, add the following code in the first tag, again just before the ending tag:

Web.sitemap;app.config
TransformXml;

IMPORTANT: Here, in the tag, you need to add the document names which you would like to make use of the transformation technology. I.E. I can put here Web.sitemap;Settings.xml;AnotherXMLDoc.xml;app.config

But do not under any circumstance change the TransformXml; line. This is a link which ensures that the transformation on the listed documents are run.

Project File Edited (1)..
Figure 4: Project File Edited (1)..

When the changes are done close the project file, right-click the project in the solution tab and select “Reload Project”. If you did not close the project file (which is open), Visual Studio will ask you if you want to close the project file first. If you select “Yes” it will reload the project but if you select “No” nothing will happen (SO SELECT YES).

4. If not already created, create your XML files, including their child files and add them to the project. In order for them to look like figure 5 you will again have to edit the project file (.csproj or .vsproj).

Project Files Hierarchy..
Figure 5: Project Files Hierarchy..

To make the above changes locate (or add if it is not already in the file) the names of the files that you created (they will be somewhere in one of the tags inside the project file). Make sure that the ‘DepentUpon’ property is set for your files. Here is an example for the above figures outcome:

Project File Edited (2)..
Figure 6: Project File Edited (2)..

That’s it. You’re done. Build your project and publish it.
If there were no changes inside the Web.sitemap file (after publishing), make sure that there is no namespace reference other than the xmlns:xdt=”http://schemas.microsoft.com/XML-Document-Transform” in the sitemap XML file(s).

The reason for this: At the moment (August of 2010) there are a few limitations of the XML Document Transformation Engine (Vishal R. Joshi, 2010) and one of these limitations is that the engine cannot handle any namespaces. So to see this in action glance at the following figures:

Web.sitemap WRONG!
Figure 7: WRONG!!

Web.sitemap Correct!
Figure 8: Correct!

Web.Debug.sitemap Correct!
Figure 9: Correct!

———–
aQuila22

Advertisements

Actions

Information

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




%d bloggers like this: