reading the xpath contained in xml using powershell

hi,

My xml contains

<xpath>configuration/appSettings/add[@key='abc'][@value]</xpath>

<value>124</value>

I want to fetch the value corresposnding to xpath "add[@key='abc']" from xml using powershell

August 27th, 2015 6:59am

That is not XML.  It is a piece of an edit command Are you reying to get a setting from an app.config file?

$node=$xml.SelectSingleNode("//configuration/appSettings/add[@key='abc']")

Free Windows Admin Tool Kit Click here and download it now
August 27th, 2015 7:15am

Hi Ravneet 

You can use the below to get the value. Here, 12 is the line number whose value i am finding out. Replace it with your own.

[xml]$ClientServiceConfig = (Get-Content "PATH to your XML config file")
$ClientServiceConfig.configuration.appSettings.add.GetValue("12").value

August 27th, 2015 7:34am

Hi Ravneet 

You can use the below to get the value. Here, 12 is the line number whose value i am finding out. Replace it with your own.

[xml]$ClientServiceConfig = (Get-Content "PATH to your XML config file")
$ClientServiceConfig.configuration.appSettings.add.GetValue("12").value

Unfortunately this won't work if the file is structured as claimed. THe entries are keyed which is why we need to use xpath ot get the correct entry by "key" value. The <value> component is apparently a subkey.

Free Windows Admin Tool Kit Click here and download it now
August 27th, 2015 7:46am

As we can see here: https://msdn.microsoft.com/en-us/library/Aa735785(v=VS.71).aspx

There are many structures that the data can be embedded in. We use an XPath query to eliminate the ambiguity. That is why the xpath above is given as incomplete. If we use the ConfigMgr API then this would all be handled. Since we are accessing the XML directly we need to search for the required key.

August 27th, 2015 8:02am

hi jrv,

That was a content from a xml. below is the complete one

<configurations>

   <configuration>

        <xpath>configuration/appSettings/add[@key='UploadPath'][@value]</xpath>

<value>abc</value>

  </configuration>

</configurations>

I have a app.config which needs to be updated as part of the build process. 

App.config contents

<configuration>

  <appsettings><add key="UploadPath" value="def"/>

</configuration>

I want to update the value(def) of UploadPath in app.config. The string 'uploadpath' will be searched from xml. In short, I am reading all the xpath's present in the xml file; fetching its value and then updating the value in web.config. 


Free Windows Admin Tool Kit Click here and download it now
August 27th, 2015 8:05am

hi mukesh,

thank you for your reply. I will try and  post result


August 27th, 2015 8:07am

hi jrv,

That was a content from a xml. below is the complete one

<configurations>

   <configuration>

        <xpath>configuration/appSettings/add[@key='abc'][@value]</xpath>

<value>abc</value>

  </configuration>

</configurations>

I have a app.config which needs to be updated as part of the build process. 

App.config contents

<configuration>

  <appsettings><add key="UploadPath" value="def"/>

</configuration>

I want to update the value(def) of UploadPath in app.config. The string 'uploadpath' will be searched from xml. In short, I am reading all the xpath's present in the xml file; fetching its value and then updating the value in web.config. 

You being misleading and vague.  What is it that needs to be updated?  Exactly.  You need to post an accurate representation of the XML.

To get the UploadPath:

$node=$xml.'//configuration/appSettings/add[@key="UploadPath"]')
$node.Attributes[1]='my new value'
$xml.Save($filename)

All strings are case sensitive.

Free Windows Admin Tool Kit Click here and download it now
August 27th, 2015 8:18am

I want to update the value(def) of UploadPath in app.config. The string 'uploadpath' will be searched from xml. In short, I am reading all the xpath's present in the xml file; fetching its value and then updating the value in web.config. 

This makes no sense. What are you reading using what tools?  You are being very vague.

August 27th, 2015 8:21am

I have corrected my xml. Sorry for the inconvenience. 

This might seems vague to you but this is my requirement. I have earlier used perl; this time I was trying to use powershell. I am working on this. Will post when done. 

Many thanks

Free Windows Admin Tool Kit Click here and download it now
August 27th, 2015 8:25am

Your XML is still ambiguous.  Please post the actual file.  What you have posted is missing half of the structure required for XML.

<?xml version="1.0"?>
<root>
<configurations>
   <configuration>
        <xpath>configuration/appSettings/add[@key='UploadPath'][@value]</xpath>
        <value>abc</value>
   </configuration>
</configurations>
<configuration>
  <appsettings><add key="UploadPath" value="def"/>
</configuration>
</root>

What is root and any other containing sections?

Is configuration at root or is it in "configurations"?

August 27th, 2015 8:30am

I know it is not the actual file. configurations is the root. I have given 1 xml and 1 app.config contents

I want the below

1. fetch the value "abc" corresponding to xpath 'configuration/appSettings/add[@key='UploadPath']' from XML

2. search the xpath 'configuration/appSettings/add[@key='UploadPath']' in app.config and updates it value with "abc"

Free Windows Admin Tool Kit Click here and download it now
August 27th, 2015 8:38am

Here is how to set the value.

$xml=[xml]@'
<root>
	<configurations>
		<configuration>
			<xpath>configuration/appSettings/add[@key='UploadPath'][@value]</xpath>
			<value>abc</value>
	   	</configuration>
	</configurations>
	<configuration>
	  	<appsettings>
	  		<add key="UploadPath" value="def"/>
	  	</appsettings>
	</configuration>
</root>
'@
$node=$xml.SelectSingleNode('//configuration/appsettings/add[@key="UploadPath"]')
$node.Value='newvalue'
$xml.Save($filename)


August 27th, 2015 8:46am

you have combined xml file and .config file.
Free Windows Admin Tool Kit Click here and download it now
August 27th, 2015 8:47am

this is my xml file

<root> <configurations> <configuration> <xpath>configuration/appSettings/add[@key='UploadPath'][@value]</xpath> <value>abc</value> </configuration> </configurations>

</root>

August 27th, 2015 8:49am

This piece:

<configuration>
    <xpath>configuration/appSettings/add[@key='UploadPath'][@value]</xpath>
    <value>abc</value>
 </configuration>

is designed incorrectly for what you are doing.

It should be like this:

$xml=[xml]@'
<root>
	<configurations>
		<configuration>
			<xpath>configuration/appSettings/add[@key='UploadPath']
			    <attribute name="newvalue" value="abc"/>
		    </xpath>
	   	</configuration>
	</configurations>
	<configuration>
	  	<appsettings>
	  		<add key="UploadPath" value="def"/>
	  	</appsettings>
	</configuration>
</root>
'@


$node=$xml.SelectSingleNode('//configurations/configuration/xpath')
$xpath=$node.'#text'
$newvalue=$node.attribute.newvalue

$node=$xml.SelectSingleNode($xpath)
$node.Value=$newvalue


$xml.Save($filename)
Or some similar structure that allows the data to remain correctly encapsulated.

Free Windows Admin Tool Kit Click here and download it now
August 27th, 2015 9:01am

this is my xml file

<root> <configurations> <configuration> <xpath>configuration/appSettings/add[@key='UploadPath'][@value]</xpath> <value>abc</value> </configuration> </configurations>

</root>

Are you saying you have two XML files?  You have to be clear.  Your statements are all vague and incomplete leaving us without any understanding of what you are asking.
August 27th, 2015 9:06am

hi jrv,

From the first post only I am saying I have 1 xml and 1 app.config. 

EXAMPLE OF XML FILE

<root> <configurations> <configuration> <xpath>configuration/appSettings/add[@key='UploadPath'][@value]</xpath> <value>abc</value> </configuration> </configurations>

</root>

EXAMPLE OF APP.CONFIG FILE

<configurations> <configuration> <xpath>configuration/appSettings/add[@key='UploadPath'][@value]</xpath> <value>abc</value> </configuration> </configurations>

I am not writing the basic XML details like the <?xml version=1.0>

Free Windows Admin Tool Kit Click here and download it now
August 27th, 2015 9:18am

hi jrv,

That was a content from a xml. below is the complete one

<configurations>

   <configuration>

        <xpath>configuration/appSettings/add[@key='UploadPath'][@value]</xpath>

<value>abc</value>

  </configuration>

</configurations>

I have a app.config which needs to be updated as part of the build process. 

App.config contents

<configuration>

  <appsettings><add key="UploadPath" value="def"/>

</configuration>

I want to update the value(def) of UploadPath in app.config. The string 'uploadpath' will be searched from xml. In short, I am reading all the xpath's present in the xml file; fetching its value and then updating the value in web.config. 


  • Edited by Ravneet_SCM Thursday, August 27, 2015 12:19 PM
August 27th, 2015 12:01pm

hi mukesh,

thank you for your reply. I will try and  post result


  • Edited by Ravneet_SCM Thursday, August 27, 2015 12:03 PM
Free Windows Admin Tool Kit Click here and download it now
August 27th, 2015 12:03pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics