Problem with Xml ModifyFile task

Jul 30, 2007 at 2:54 PM
Edited Jul 30, 2007 at 2:55 PM
I'm trying to update a web.config file using the ModifyFile task. With simple xml files, the task works OK, but where there is an xmlns definition in the config file, I'm unable to get the task to work. The xml file looks like:

<?xml version="1.0"?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<property name="connection.connection_string">Existing Value</property>

and the msbuild file I'm testing with:

<Project DefaultTargets="Modify" xmlns="">
<Import Project="$(MSBuildExtensionsPath)\Microsoft\SdcTasks\Microsoft.Sdc.Common.tasks" />
<XmlNamespace Include="NHibernateNamespace">
<Target Name="Modify">
<Message Text="@(XmlNamespace->'%(Uri)')" />
XPath="/configuration/hibernate-configuration/session-factory/property@name = 'connection.connection_string'"
NewValue="New Value"
Namespace="@(XmlNamespace)" />

Basically, the build file runs OK without any errors but the connection.connection_string element is not updated. Does anybody know how I should be specifying the Namespace task item group?
Jul 31, 2007 at 4:41 PM
Managed to fix this in the end. The trick was to assign a dummy prefix for the namespace:

<XmlNamespace Include="NHibernateNamespace">

and then define the XPath as follows:

XPath="/configuration/nh:hibernate-configuration/nh:session-factory/nh:property@name = 'connection.connection_string'"

Jan 8, 2011 at 7:42 AM

you can also remove the namespace from the XML prior to Xml.ModifyFile after your modification you can add back the namespace.  You can use File.Replace to remove and add the name space.  It is just one more work around.

  • Remove the Namespace from target XML using File.Replace
  • Perform Xml.ModifyFile
  • Add back the Namespace attribute to the target XML.