Connecting Tech Pros Worldwide Help | Site Map

how to change xml node property by c# code

Newbie
 
Join Date: Mar 2007
Posts: 4
#1: Mar 27 '07
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:nhibernate-configuration-2.0" >
<session-factory name="NHibernate.Test">
<!-- properties -->
<property name="connection.provider">NHibernate.Connection.D riverConnectionProvider</property>
<property name="connection.driver_class">NHibernate.Driver.O racleDataClientDriver</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>[/html]



i want to change dynamically the connection.connection_string string property

thanks alot

ron

NOTE: Please add code or html tags to samples. Thanks, moderator.
dorinbogdan's Avatar
Expert
 
Join Date: Feb 2007
Posts: 822
#2: Mar 27 '07

re: how to change xml node property by c# code


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 SelectSingleNode() method and to update the InnerText property of the selected XmlNode. Then call the Save() method.

I hope to succeed. Otherwise, let us know.
Newbie
 
Join Date: Mar 2007
Posts: 4
#3: Mar 27 '07

re: how to change xml node property by c# code


OK
for note this is the code which i wrote
Expand|Select|Wrap|Line Numbers
  1. XmlDocument xmlDoc = new XmlDocument();
  2.  
  3. xmlDoc.Load("hibernate.cfg.xml");
  4.  
  5. XmlNamespaceManager nm = new XmlNamespaceManager(xmlDoc.NameTable);
  6.  
  7. nm.AddNamespace("HC", "urn:nhibernate-configuration-2.0");
  8.  
  9. XmlNode node = xmlDoc.SelectSingleNode("//HC:session-factory[@name='NHibernate.Test']/HC:Property[@name='connection.connection_string']",nm);
  10.  
  11. node.InnerText = "New Value";
  12.  
  13. xmlDoc.Save("hibernate.cfg.xml");

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


Quote:

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 SelectSingleNode() method and to update the InnerText property of the selected XmlNode. Then call the Save() method.

I hope to succeed. Otherwise, let us know.

dorinbogdan's Avatar
Expert
 
Join Date: Feb 2007
Posts: 822
#4: Mar 27 '07

re: how to change xml node property by c# code


use property, not Property, to look like this:
Expand|Select|Wrap|Line Numbers
  1. xmlDoc.SelectSingleNode("//HC:session-factory[@name='NHibernate.Test']/HC:property[@name='connection.connection_string']",nm);
  2.  
Newbie
 
Join Date: Mar 2007
Posts: 4
#5: Mar 27 '07

re: how to change xml node property by c# code


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


Quote:

Originally Posted by dorinbogdan

use property, not Property, to look like this:

Expand|Select|Wrap|Line Numbers
  1. xmlDoc.SelectSingleNode("//HC:session-factory[@name='NHibernate.Test']/HC:property[@name='connection.connection_string']",nm);
  2.  

dorinbogdan's Avatar
Expert
 
Join Date: Feb 2007
Posts: 822
#6: Mar 27 '07

re: how to change xml node property by c# code


Thanks,
You're welcome.
God bless you.
Dorin.
Reply