c# - Read and edit XML value -


i want read , edit value string (value="j:\demo\demo_data_3.xml") next parameter name="database".

when use

xpathdocument xpathdoc = new xpathdocument(dashboardpath); xpathnavigator navigator = xpathdoc.createnavigator();  while (navigator.movetofollowing("parameters", "")) 

i can move to <parameter> not read or edit values. have advice?

xml source

<?xml version="1.0" encoding="utf-8"?> <dashboard currencyculture="en-us">   <title text="dashboard" />   <datasources>     <sqldatasource componentname="dashboardsqldatasource1">       <name>demo_data_excel</name>       <connection name="testdata" providerkey="inmemorysetfull">         <parameters>           <parameter name="database" value="j:\demo\demo_data_3.xml" />           <parameter name="read only" value="1" />           <parameter name="generateconnectionhelper" value="false" />         </parameters>       </connection>       <query type="tablequery" name="data">         <table name="data">           <column name="market segment" />           <column name="market subsegmt" />           <column name="customer" />                 </table>       </query>       <resultschema>         <dataset name="sql data source 1">           <view name="data">             <field name="market segment" type="string" />             <field name="market subsegmt" type="string" />                       </view>         </dataset>       </resultschema>     </sqldatasource>   </datasources> </dashboard> 

two possible ways of doing given have file called testfile.xml next program's exe.

first:

var xdocument = xdocument.load("testfile.xml");  xdocument.xpathselectelement(     "/dashboard/datasources/sqldatasource/connection/parameters/parameter[@name='database']")     .attribute("value").setvalue("updatedpath");  xdocument.save("testfile_updated.xml"); 

second:

var xdocument = xdocument.load("testfile.xml");  xdocument.root.elements("datasources")     .elements("sqldatasource")     .elements("connection")     .elements("parameters")     .elements("parameter")     .single(p => p.attribute("name").value == "database")     .attribute("value")     .setvalue("updatedpath");  xdocument.save("testfile_updated.xml"); 

the second 1 assumes have 1 parameter node name="database". can use these examples better suit individual needs.


Comments

Popular posts from this blog

c - How to retrieve a variable from the Apache configuration inside the module? -

c# - Constructor arguments cannot be passed for interface mocks -

python - malformed header from script index.py Bad header -