Jump to content
tonyrocks

How to modify Web.config Key nodes and attribute values

Recommended Posts

tonyrocks

I'm really wanting to change the values that are set in a default Web.config file (created by my .NET developers) and then save it.   The keys are typically specific to their local machines.  I want to take what they send me and change the attribute value.   Here is my Web.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="infragistics.web" type="System.Configuration.SingleTagSectionHandler,System, Version=1.0.3300.0, Culture=neutral" />
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
  </configSections>

<appSettings>
    <add key="LocalProdmode" value="None" lockItem="false" />
</appSetting>

  <connectionStrings>
    <add name="ProdDatabase" connectionString="Data Source=localhost;Initial Catalog=Products;Trusted_Connection=False;User ID=productadmin;Password=apple123;" />
    <add name="Prod.box.Web" connectionString="Data Source=localhost;Initial Catalog=Products;Trusted_Connection=False;User ID=productadmin;Password=apple123;" />   
  </connectionStrings>
  <system.diagnostics>
    <trace>
      <listeners>
        <add initializeData="c:\logs\products.Log" type="Prod.Diagnostics.LogTraceListener, Prod" name="Prod.box.log" traceOutputOptions="DateTime, Timestamp, ProcessId, ThreadId">
          <filter type="System.Diagnostics.EventTypeFilter" initializeData="Information" />
        </add>
      </listeners>
    </trace>
  </system.diagnostics>

</configuration>

What I need to do is changed the line:

<add key="LocalProdmode" value="None" lockItem="false" />

to

<add key="LocalProdmode" value="555" lockItem="true" />

and I need to change the lines:

<add name="ProdDatabase" connectionString="Data Source=localhost;Initial Catalog=Products;Trusted_Connection=False;User ID=productadmin;Password=apple123;" />
    <add name="Prod.box.Web" connectionString="Data Source=localhost;Initial Catalog=Products;Trusted_Connection=False;User ID=productadmin;Password=apple123;" />

to

<add name="ProdDatabase" connectionString="Data Source=172.16.18.2;Initial Catalog=ProductsProd4;Trusted_Connection=False;User ID=admin;Password=orange123;" />
    <add name="Prod.box.Web" connectionString="Data Source=l72.16.18.2;Initial Catalog=ProductsProd3;Trusted_Connection=False;User ID=admin;Password=orange123;" />

and finally I need to change the line:

<add initializeData="c:\logs\products.Log" type="Prod.Diagnostics.LogTraceListener, Prod" name="Prod.box.log" traceOutputOptions="DateTime, Timestamp, ProcessId, ThreadId">

to

<add initializeData="c:\productionlog\products.Log" type="Prod.Diagnostics.LogTraceListener, Prod" name="Prod.quad.log" traceOutputOptions="DateTime, Timestamp, ProcessId, ThreadId">

and then overwrite (or even save) the file with the changes.

I'm guessing I have to use something like #include <_XMLDomWrapper.au3> or something?  I'm hoping and AutoIt can help me.  I wish I could have found this tool earlier in my career! :(

-Tony

Share this post


Link to post
Share on other sites
jdelaney

1: your xml is malformed:

<appSettings>
    <add key="LocalProdmode" value="None" lockItem="false" />
</appSetting>


needs to be:
<appSettings>
    <add key="LocalProdmode" value="None" lockItem="false" />
</appSettings>

Here is the basics of grabbing an element, and updating attributes...try it out

$sLocalXMLFile = @ScriptDir & "\some.xml"
$oXML = ObjCreate("Microsoft.XMLDOM")
$oXML.load($sLocalXMLFile & @CRLF)

$oKey = $oXML.selectSingleNode("//add[@key='LocalProdmode']")
ConsoleWrite($oKey.xml & @CRLF)
$oKey.setAttribute("value","555")
$oKey.setAttribute("lockItem","true")
ConsoleWrite($oKey.xml & @CRLF)

$oXML.save("c:\whereveryouwanttosaveto.xml")

output (shows the modification to the attributes):

<add key="LocalProdmode" value="None" lockItem="false"/>
<add key="LocalProdmode" value="555" lockItem="true"/>


IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.

Share this post


Link to post
Share on other sites
tonyrocks

wow...awesome.  This is exactly what I needed! Much simpler than what I could have imagined.

Share this post


Link to post
Share on other sites
raji

Hi,

$sLocalXMLFile = @ScriptDir & "\some.xml"
$oXML = ObjCreate("Microsoft.XMLDOM")
$oXML.load($sLocalXMLFile & @CRLF)

$oKey = $oXML.selectSingleNode("//add[@key='LocalProdmode']")
ConsoleWrite($oKey.xml & @CRLF)
$oKey.setAttribute("value","555")
$oKey.setAttribute("lockItem","true")
ConsoleWrite($oKey.xml & @CRLF)

$oXML.save("c:\whereveryouwanttosaveto.xml")

this code is not working for me... wat are all the library file to be added..?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.