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




CommandArgument concatenation in VS2010

15 06 2010

Just a quick look on how i got my command argument concatenation to work in VS2010. My code in the .aspx page:

<asp:ImageButton ID=”btnBidRemove” runat=”server” AlternateText=”Verwijder” ImageUrl=”~/Images/remove.png” ToolTip=”Verwijder” onclick=”butRemove_Click” CommandArgument=’<%# DataBinder.Eval(Container.DataItem,”dbid”) + “|” + DataBinder.Eval(Container.DataItem,”adid”) %> /> 
As simple as that! 

 ———-
aQuila22
 

 





ASP.NET & a Regular Expression Validator

30 05 2010

Right so this is my second blog.  Just last week I was struggling with using an ASP.NET built in validator control to validate a text box control for values between certain amounts and that it must be a decimal number (I also had one for “required field”).

My mission was to join all three seperate validation controls into one but in the end only joined two of them which was the “between values” and “must be decimals”.  I could also join the third one but was lazy so don’t judge me.

I tried to first see if I can join, in a regular expression,  the “only decimals” and “required field” validations but found that only the “required field” validator control can return a true or false for null or !null text boxes.  So it didn’t matter even if i tried more because this block me instantly (I’m using the validation control where you can add your own regular expression for the validation criteria). 

I then tried to see if I can join the “only decimal” and “between values” validator controls with a regular expression but in the end this was a dead end because my “between values” is set dynamically through my code behind page from database values.  If one were able to build such an expression with dynamically set “between values” it would be an enormous not to mention VERY complicated expression. 

So with all this done I decided to just use the ASP.NET custom validator control and use client script to do the validation for both the “decimal only” and “between values”.  Following is a sample of my code:

<asp:TextBox ID=”txtBidAmount” CssClass=”invoer” runat=”server” CausesValidation=”true” ></asp:TextBox>
           

In my ASPX page:

<script type=”text/javascript”>
      function CheckPrime(sender, args) {
          args.IsValid = true;
         
          var bid = document.getElementById(‘<%=txtBidAmount.ClientID %>’).value;
          var validateMin = document.getElementById(‘<%=hidValidatorMin.ClientID %>’).value;
          var validateMax = document.getElementById(‘<%=hidValidatorMax.ClientID %>’).value;
         
          if (bid.match(/^\d+(,)?\d{0,2}?$/) == null)
          { args.IsValid = false; }

          if (parseInt(bid) ‘SmallerThan’ parseInt(validateMin))
{ args.IsValid = false; }

if (parseInt(bid) ‘GreaterThan’ parseInt(validateMax))
{ args.IsValid = false; }
         
          return;         
      }
  </script>
        <asp:RequiredFieldValidator ID=”BidValidatorNotNull” runat=”server”
        ControlToValidate=”txtBidAmount”
        meta:resourcekey=”BidValidatorNotNullRes”>
        </asp:RequiredFieldValidator>
       
        <asp:CustomValidator ID=”BidCustomValidator”
        ControlToValidate=”txtBidAmount”
        ClientValidationFunction=”CheckPrime”
        meta:resourcekey=”BidCustomValidatorRes”
        runat=”server”></asp:CustomValidator>

———–
aQuila22