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
Technology Tips and News
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
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']")
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
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.
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.
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.
hi mukesh,
thank you for your reply. I will try and post result
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.
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.
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
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"?
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"
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)
this is my xml file
<root> <configurations> <configuration> <xpath>configuration/appSettings/add[@key='UploadPath'][@value]</xpath> <value>abc</value> </configuration> </configurations>
</root>
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.
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.this is my xml file
<root> <configurations> <configuration> <xpath>configuration/appSettings/add[@key='UploadPath'][@value]</xpath> <value>abc</value> </configuration> </configurations>
</root>
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>
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.
hi mukesh,
thank you for your reply. I will try and post result