Problem while replacing value in Xml file.

Jun 2, 2008 at 6:53 PM
Hi,
I have config file as follows:
<?xml version="1.0" encoding="utf-8"?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.0">
  <session-factory name="NHibernate.CreativeManagementDataAccess">
    <property name="hibernate.connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
    <property name="hibernate.connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
    <property name="hibernate.dialect">NHibernate.Dialect.MsSql2000Dialect</property>
    <property name="hibernate.connection.connection_string">Old Value To Be Replaced</property>
    <mapping assembly="CreativeManagementDataAccess" />
  </session-factory>
</hibernate-configuration>

Ans I need to replace or change the values of properties mentioned in above files at runtime. For the same I have created new proj file as follows:

<Project DefaultTargets="SetupEnvironment" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">  
 <Import Project="bin\Microsoft.Sdc.Common.tasks"/> 
 <PropertyGroup>
        <appname>SetupEnvironmentApplication</appname>
    </PropertyGroup> 
 <ItemGroup>
  <XmlNamespace Include="NHibernateNamespace">
   <Prefix>nh</Prefix>
   <Uri>urn:nhibernate-configuration-2.0</Uri>
  </XmlNamespace>
 </ItemGroup> 
 <Target Name = "SetupEnvironment">
  <Message Text="In SetupEnvironment target..."/>
  <Message Text="@(XmlNamespace->'%(Uri)')" />
  <Xml.ModifyFile Path="S:\MSBuildProject\SampleConfigFile.xml" XPath="/nh:hibernate-configuration/nh:session-factory/nh:property@name='hibernate.connection.connection_string'" NewValue="Data Source=localhost;Initial Catalog=CreativeManagement;Integrated Security=True" Namespace="@(XmlNamespace)" />
 </Target>  
</Project>

When I am building this project as :
msbuild.exe ProjectFilePath\ProjectFileName.proj
its giving me error that
error : Message    = '/nh:hibernate-configuration/nh:session-factory/nh:property@name='hibernate.connection.connection_string'' has an invalid token.

Anybody is having any idea why this error is coming for me.
Thanks in advance.
__ Abhijit

Developer
Jun 2, 2008 at 10:48 PM
you are missing square braces for the attribute

Use this: <Xml.ModifyFile Path="C:\asomeother\xmlreplace.xml" XPath="/nh:hibernate-configuration/nh:session-factory/nh:property[@name='hibernate.connection.connection_string']" NewValue="b" Namespace="@(XmlNamespace)" />

Result:

<?xml version="1.0" encoding="utf-8"?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.0">
  <session-factory name="NHibernate.CreativeManagementDataAccess">
    <property name="hibernate.connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
    <property name="hibernate.connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
    <property name="hibernate.dialect">NHibernate.Dialect.MsSql2000Dialect</property>
    <property name="hibernate.connection.connection_string">Data Source=localhost;Initial Catalog=CreativeManagement;Integrated Security=True</property>
    <mapping assembly="CreativeManagementDataAccess" />
  </session-factory>
</hibernate-configuration>
Jun 2, 2008 at 11:21 PM
Yes... my problem is resolved.
Thanks Lot!!
__ Abhijit