how to change xml node property by c# code

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ronlahav
    New Member
    • Mar 2007
    • 4

    #1

    how to change xml node property by c# code

    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)

    [html]<?xml version="1.0" encoding="utf-8" ?>
    <hibernate-configuration xmlns="urn:nhib ernate-configuration-2.0" >
    <session-factory name="NHibernat e.Test">
    <!-- properties -->
    <property name="connectio n.provider">NHi bernate.Connect ion.DriverConne ctionProvider</property>
    <property name="connectio n.driver_class" >NHibernate.Dri ver.OracleDataC lientDriver</property>
    <property name="connectio n.connection_st ring">User ID=th;password= manager;Data Source=th02;Per sist Security Info=False</property>
    <property name="show_sql" >true</property>
    <property name="dialect"> NHibernate.Dial ect.Oracle9Dial ect</property>
    <property name="use_outer _join">true</property>
    <property name="query.sub stitutions">tru e 1, false 0, yes 'Y', no 'N'</property>
    <!-- mapping files -->
    <mapping assembly="Pla.P er"/>
    <mapping assembly="Int.P er"/>
    <mapping assembly="Pen.M ap"/>

    </session-factory>
    </hibernate-configuration>[/html]



    i want to change dynamically the connection.conn ection_string string property

    thanks alot

    ron

    NOTE: Please add code or html tags to samples. Thanks, moderator.
    Last edited by dorinbogdan; Mar 27 '07, 10:39 AM. Reason: added html tags
  • dorinbogdan
    Recognized Expert Contributor
    • Feb 2007
    • 839

    #2
    Welcome to TheScripts TSDN...

    You may use XMLDocument class.
    See these links:
    Manipulate XML data with XPath and XmlDocument
    and
    Modify XML Data by Using DOM in .NET Framework

    You need to use SelectSingleNod e() method and to update the InnerText property of the selected XmlNode. Then call the Save() method.

    I hope to succeed. Otherwise, let us know.

    Comment

    • ronlahav
      New Member
      • Mar 2007
      • 4

      #3
      OK
      for note this is the code which i wrote
      Code:
      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='connection.connection_string']",nm);
      
      node.InnerText = "New Value";
      
      xmlDoc.Save("hibernate.cfg.xml");

      but i have all the time node = null that means that something in the Xpath Query is incorrect
      Ron


      Originally posted by dorinbogdan
      Welcome to TheScripts TSDN...

      You may use XMLDocument class.
      See these links:
      Manipulate XML data with XPath and XmlDocument
      and
      Modify XML Data by Using DOM in .NET Framework

      You need to use SelectSingleNod e() method and to update the InnerText property of the selected XmlNode. Then call the Save() method.

      I hope to succeed. Otherwise, let us know.
      Last edited by dorinbogdan; Mar 27 '07, 12:02 PM. Reason: added code tags

      Comment

      • dorinbogdan
        Recognized Expert Contributor
        • Feb 2007
        • 839

        #4
        use property, not Property, to look like this:
        Code:
        xmlDoc.SelectSingleNode("//HC:session-factory[@name='NHibernate.Test']/HC:property[@name='connection.connection_string']",nm);

        Comment

        • ronlahav
          New Member
          • Mar 2007
          • 4

          #5
          Wow - you are amazing - how could i resolve this problem????!?!! ? (you cant debug Xpath)
          i will recommand you to be an XML MVP :)


          Originally posted by dorinbogdan
          use property, not Property, to look like this:
          Code:
          xmlDoc.SelectSingleNode("//HC:session-factory[@name='NHibernate.Test']/HC:property[@name='connection.connection_string']",nm);

          Comment

          • dorinbogdan
            Recognized Expert Contributor
            • Feb 2007
            • 839

            #6
            Thanks,
            You're welcome.
            God bless you.
            Dorin.

            Comment

            Working...