none
how to change xml node property in by c# code

    Question

  • Dear all

    i Wonder how can i edit a node property (in C# not by hand :) )

    i have the follwing XMl file (hibernate.cfg.xml)

    <?xml version = "1.0" encoding = "utf-8"?>

    <hibernate-configuration xmlns = "urn:nhibernate-configuration-2.0">

       <session-factory name ="NHibernate.Test>

          <Property name = "connectio.connection_string"> user ID = ...</property>

          <Property name = "connectio.provider"> ..........</property>

      </session-factory>

    </hibernate-configuration>

     

    i want to change dynamically the connection string property

    thanks alot

    ron

    Monday, March 26, 2007 8:09 AM

Answers

  • XmlDocument xmlDoc = new XmlDocument();

    xmlDoc.Load("hibernate.cfg.xml");

    XmlNamespaceManager nm = new XmlNamespaceManager(xmlDoc.NameTable);

    nm.AddNamespace("HC", "urn:nhibernate-configuration-2.0");

    XmlNode node = xmlDoc.SelectSingleNode("//HC:session-factory[@name='NHibernate.Test']/HC:Property[@name='connectio.connection_string']",nm);

    node.InnerText = "New Value";

    xmlDoc.Save("hibernate.cfg.xml");

    *Note "connectio.connection_string" is a miss spell and it is kept in my sample code.

    Monday, March 26, 2007 8:47 AM

All replies

  • XmlDocument xmlDoc = new XmlDocument();

    xmlDoc.Load("hibernate.cfg.xml");

    XmlNamespaceManager nm = new XmlNamespaceManager(xmlDoc.NameTable);

    nm.AddNamespace("HC", "urn:nhibernate-configuration-2.0");

    XmlNode node = xmlDoc.SelectSingleNode("//HC:session-factory[@name='NHibernate.Test']/HC:Property[@name='connectio.connection_string']",nm);

    node.InnerText = "New Value";

    xmlDoc.Save("hibernate.cfg.xml");

    *Note "connectio.connection_string" is a miss spell and it is kept in my sample code.

    Monday, March 26, 2007 8:47 AM
  • Hi

    First i really appreciate your help!!!

    i have problem with the XmlNode

    regarding to your note i have had spelled wrong "connectio.connection_string" - u r right - i  fixed it.

    when i execute the folowing line i get null result for the xPath

    XmlNode node = xmlDoc.SelectSingleNode("//HC:session-factory[@name='NHibernate.Test']/HC:Property[@name='connectio.connection_string']",nm);

    i executed the line without the last part of the query (in the immediate window)

    xmlDoc.SelectSingleNode("//HC:session-factory[@name='NHibernate.Test']", nm);

    and i got result!

    so how do i get access to the specific name??? ("'connection.connection_string')

    any idea???

     

    thanks alot

     

     

    Monday, March 26, 2007 10:27 AM
  • Hi,

    Here is a good tool for testing xpaths

    http://download.microsoft.com/download/mdac26/utility/2.8/win98/en-us/xpath.exe

    I simplified your xml doc a bit, e.g. removed the namespace

    <?xml version="1.0"?>
    <hibernate-configuration>
       <session-factory name ="NHibernate.Test">
          <Property name="a">foo</Property>
          <Property name="b">bar</Property>
      </session-factory>
    </hibernate-configuration>

    Then try these 2 xpaths, my guess is one of them does the trick for you.

    /hibernate-configuration/session-factory/Property/@name
    /hibernate-configuration/session-factory/Property[@name='a']/text()

    Monday, March 26, 2007 2:23 PM
  • I tested my code with the XML you posted (after I fixed some obvious typos such as missing quotes) and it worked well for me. If you could post your complete XML I can help you with what's not matching. XML is case-sensitive so you have to make sure everything is spelled exactly right.

    About "connectio.connection_string" -- did you fix BOTH the xml and the XPath query? They have to match.

    Monday, March 26, 2007 5:26 PM
  • Good morning very friendly ........

    OK

    So i copy and past the  xml file - the scond part of the xml Xpath still does not work

    thanks for the help

    i want to change the red line (the connection string)

     

     

     

     

    <?xml version="1.0" encoding="utf-8" ?>
    <hibernate-configuration  xmlns="urn:nhibernate-configuration-2.0" >
     <session-factory name="NHibernate.Test">
      <!-- properties -->
      <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
      <property name="connection.driver_class">NHibernate.Driver.OracleDataClientDriver</property>
      <property name="connection.connection_string">User ID=th;password=manager;Data Source=th02;Persist Security Info=False</property>
      <property name="show_sql">true</property>
      <property name="dialect">NHibernate.Dialect.Oracle9Dialect</property>
      <property name="use_outer_join">true</property>
      <property name="query.substitutions">true 1, false 0, yes 'Y', no 'N'</property>
      <!-- mapping files -->
      <mapping assembly="Pla.Per"/>
      <mapping assembly="Int.Per"/>
      <mapping assembly="Pen.Map"/>
     
     </session-factory>
    </hibernate-configuration>

     

    for your question

    did you fix BOTH the xml and the XPath query? They have to match

    you can see in the body of the xml the match between the xml and your code (in shortage there was a match!)


     

     

     

     

     

     

     

    Tuesday, March 27, 2007 6:43 AM
  • OK

    so i resolved the prblem

    XmlNode node = xmlDoc.SelectSingleNode("//HC : session-factory[@name='NHibernate.Test']/HC : Property[@name='connectio.connection_string']",nm);

     

    the Property should be property - i didnt know that the Xpath is case sensitive!!!

    How ever can you explain to me why did we use AddNamespace method???

    some words about the code???

     

    why did it help me?

     

    and again - thanks alot - i will re\command Microsoft to give you xml MVP Smile

     

    Tuesday, March 27, 2007 12:01 PM
  • Tuesday, March 27, 2007 2:58 PM
  •  ronlahav wrote:

    How ever can you explain to me why did we use AddNamespace method???

     

    The elements in the XML sample document are in a namespace with the namespace URI urn:nhibernate-configuration-2.0 and to use that with XPath you need to bind a prefix to the namespace URI which the AddNamespace method call does. That way in the XPath expression you use a prefix (e.g. 'HC') that is bound to the namespace URI.

    Tuesday, March 27, 2007 3:02 PM
  • OK

    First thanks for the reply

    so the situation is this

    My Xml has changed

    here is its structure

     

    <?xml version="1.0" encoding="utf-8" ?>

    <objects xmlns="http://www.springframework.net" >

    <object id="bus" type="aa.bb.Msmq.MsmqBus, aa.bb.Msmq">

     

    <property name="NumberOfWorkerThreads" value="7" />

    <property name="InputQueue" value="appserver" />

    <property name="SubscriptionStorageQueue" value="appserver-subscriptions" />

    <property name="Builder" ref="Builder" />

    <property name="PublishKeepAlives" value="true" />

    <property name="KeepAliveIntervalInSeconds" value="30" />

    <property name="IsTransactional" value="false" />

    <property name="TransctionTimeSpanInSeconds" value="12000" />

    <property name="PurgeOnStartup" value="false" />

     

     

    </< FONT>object>

    </< FONT>objects>

     

     

    My goal is to change the InputQueue value.  I wrote the folowing code and i have problems with the Xpath

     

    and i also have problem changing the value in the tag

     

    <property name="InputQueue" value="appserver" />

    i need to change th "appserver"  value inside the tag the code whiche i wrote chnging it as folows

     

    <property name="InputQueue" value="appserver" /> NewValue </property>

     

    and i need

     

    <property name="InputQueue" value="NewValue" />

     

     

     

     

    Code Snippet

     

    XmlDocument xmlDoc = new XmlDocument();

    xmlDoc.Load("hibernate.cfg.xml");

    XmlNamespaceManager nm = new XmlNamespaceManager(xmlDoc.NameTable);

    nm.AddNamespace("HC", "http://www.springframework.net");

    XmlNode node = xmlDoc.SelectSingleNode("//HC: objects[@id='bus']/HC:property[@name='InputQueue']",nm);

    node.InnerText = "New Value";

    xmlDoc.Save("hibernate.cfg.xml");

     

     

     

     

     

     

     

    How can i change the Node value???

     

    even when i trim my x path to

    //HCSurprisebjects[@id='bus']

    i have got null

    can you Hlp me Please!!!!!

     

     

     

     

    Tuesday, April 17, 2007 11:29 AM