This project is read-only.

Problem with Xml ModifyFile task

Jul 30, 2007 at 3:54 PM
Edited Jul 30, 2007 at 3: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"?>
<configuration>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property name="connection.connection_string">Existing Value</property>
</session-factory>
</hibernate-configuration>
</configuration>

and the msbuild file I'm testing with:

<Project DefaultTargets="Modify" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\Microsoft\SdcTasks\Microsoft.Sdc.Common.tasks" />
<ItemGroup>
<XmlNamespace Include="NHibernateNamespace">
<Prefix></Prefix>
<Uri>urn:nhibernate-configuration-2.2</Uri>
</XmlNamespace>
</ItemGroup>
<Target Name="Modify">
<Message Text="@(XmlNamespace->'%(Uri)')" />
<Xml.ModifyFile
Path="C:\Temp\ModifyFile\test.xml"
XPath="/configuration/hibernate-configuration/session-factory/property@name = 'connection.connection_string'"
NewValue="New Value"
Namespace="@(XmlNamespace)" />
</Target>
</Project>

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 5:41 PM
Managed to fix this in the end. The trick was to assign a dummy prefix for the namespace:

<ItemGroup>
<XmlNamespace Include="NHibernateNamespace">
<Prefix>nh</Prefix>
<Uri>urn:nhibernate-configuration-2.2</Uri>
</XmlNamespace>
</ItemGroup>

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 8: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.