How to change value in XML from another file with PowerShell?
up vote
-1
down vote
favorite
I have config. I need to change values of value in a config having received value from the text file.
That is: the XML file has a <add key='shopId' value='foo'/>
and a text file, it has a value.
I have replaced either the entire file, or prescribed duplicate value, or changed to the value that is specified in the variable, but not from a text file.
I do
[XML]$xml = Get-Content "C:Folderconfig"
$xpath = "/configuration/appSettings/add[@value='5' and @key ='shopId']"
$nodes = $xml.SelectNodes($xpath)
foreach ($n in $nodes) {
$n.value = "TEST" # How to change value from another file?
}
$xml.Save($xmlFile)
But it's not correct for me. Please help me. How to change value from another file?
xml powershell
add a comment |
up vote
-1
down vote
favorite
I have config. I need to change values of value in a config having received value from the text file.
That is: the XML file has a <add key='shopId' value='foo'/>
and a text file, it has a value.
I have replaced either the entire file, or prescribed duplicate value, or changed to the value that is specified in the variable, but not from a text file.
I do
[XML]$xml = Get-Content "C:Folderconfig"
$xpath = "/configuration/appSettings/add[@value='5' and @key ='shopId']"
$nodes = $xml.SelectNodes($xpath)
foreach ($n in $nodes) {
$n.value = "TEST" # How to change value from another file?
}
$xml.Save($xmlFile)
But it's not correct for me. Please help me. How to change value from another file?
xml powershell
1
It's not clear to me what problem you're facing. Does assigning the value not work? Are you asking how to read text from a file? Something else entirely? Are you getting errors? If so, what do they say?
– Ansgar Wiechers
Nov 14 at 11:18
Yes, i don't rewrite value from my text file. If i put $n.value = "1, or another", config file is update, but does not take the values from my text file.
– ArtemKiryanov
Nov 14 at 12:02
I would like to rewrite value from my text file.
– ArtemKiryanov
Nov 14 at 12:04
$n.value = "TEST"
->$n.value = Get-Content 'C:pathtofile.txt'
– Ansgar Wiechers
Nov 14 at 12:09
add a comment |
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
I have config. I need to change values of value in a config having received value from the text file.
That is: the XML file has a <add key='shopId' value='foo'/>
and a text file, it has a value.
I have replaced either the entire file, or prescribed duplicate value, or changed to the value that is specified in the variable, but not from a text file.
I do
[XML]$xml = Get-Content "C:Folderconfig"
$xpath = "/configuration/appSettings/add[@value='5' and @key ='shopId']"
$nodes = $xml.SelectNodes($xpath)
foreach ($n in $nodes) {
$n.value = "TEST" # How to change value from another file?
}
$xml.Save($xmlFile)
But it's not correct for me. Please help me. How to change value from another file?
xml powershell
I have config. I need to change values of value in a config having received value from the text file.
That is: the XML file has a <add key='shopId' value='foo'/>
and a text file, it has a value.
I have replaced either the entire file, or prescribed duplicate value, or changed to the value that is specified in the variable, but not from a text file.
I do
[XML]$xml = Get-Content "C:Folderconfig"
$xpath = "/configuration/appSettings/add[@value='5' and @key ='shopId']"
$nodes = $xml.SelectNodes($xpath)
foreach ($n in $nodes) {
$n.value = "TEST" # How to change value from another file?
}
$xml.Save($xmlFile)
But it's not correct for me. Please help me. How to change value from another file?
xml powershell
xml powershell
edited Nov 14 at 11:20
Ansgar Wiechers
138k12120182
138k12120182
asked Nov 14 at 11:14
ArtemKiryanov
51
51
1
It's not clear to me what problem you're facing. Does assigning the value not work? Are you asking how to read text from a file? Something else entirely? Are you getting errors? If so, what do they say?
– Ansgar Wiechers
Nov 14 at 11:18
Yes, i don't rewrite value from my text file. If i put $n.value = "1, or another", config file is update, but does not take the values from my text file.
– ArtemKiryanov
Nov 14 at 12:02
I would like to rewrite value from my text file.
– ArtemKiryanov
Nov 14 at 12:04
$n.value = "TEST"
->$n.value = Get-Content 'C:pathtofile.txt'
– Ansgar Wiechers
Nov 14 at 12:09
add a comment |
1
It's not clear to me what problem you're facing. Does assigning the value not work? Are you asking how to read text from a file? Something else entirely? Are you getting errors? If so, what do they say?
– Ansgar Wiechers
Nov 14 at 11:18
Yes, i don't rewrite value from my text file. If i put $n.value = "1, or another", config file is update, but does not take the values from my text file.
– ArtemKiryanov
Nov 14 at 12:02
I would like to rewrite value from my text file.
– ArtemKiryanov
Nov 14 at 12:04
$n.value = "TEST"
->$n.value = Get-Content 'C:pathtofile.txt'
– Ansgar Wiechers
Nov 14 at 12:09
1
1
It's not clear to me what problem you're facing. Does assigning the value not work? Are you asking how to read text from a file? Something else entirely? Are you getting errors? If so, what do they say?
– Ansgar Wiechers
Nov 14 at 11:18
It's not clear to me what problem you're facing. Does assigning the value not work? Are you asking how to read text from a file? Something else entirely? Are you getting errors? If so, what do they say?
– Ansgar Wiechers
Nov 14 at 11:18
Yes, i don't rewrite value from my text file. If i put $n.value = "1, or another", config file is update, but does not take the values from my text file.
– ArtemKiryanov
Nov 14 at 12:02
Yes, i don't rewrite value from my text file. If i put $n.value = "1, or another", config file is update, but does not take the values from my text file.
– ArtemKiryanov
Nov 14 at 12:02
I would like to rewrite value from my text file.
– ArtemKiryanov
Nov 14 at 12:04
I would like to rewrite value from my text file.
– ArtemKiryanov
Nov 14 at 12:04
$n.value = "TEST"
-> $n.value = Get-Content 'C:pathtofile.txt'
– Ansgar Wiechers
Nov 14 at 12:09
$n.value = "TEST"
-> $n.value = Get-Content 'C:pathtofile.txt'
– Ansgar Wiechers
Nov 14 at 12:09
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
accepted
From what I understand you want to read the value from a text file and insert it into your XML configuration file.
MWE
$TextFile = "C:Folderfile.txt"
$XMLFile = "C:Folderconfig.xml"
$Value = Get-Content -Path $TextFile -Raw
[XML]$XML = Get-Content -Path $XMLFile
$XPath = "/configuration/appSettings/add[@value='5' and @key ='shopId']"
$Nodes = $XML.SelectNodes($xpath)
foreach ($Node in $Nodes) {
$Node.value = $Value
}
$XML.Save($XMLFile)
Cannot set "value" because only strings can be used as values to set XmlNode properties.
– ArtemKiryanov
Nov 14 at 13:24
Thank you. I fixed.
– ArtemKiryanov
Nov 14 at 13:53
@ArtemKiryanov Please mark this as the answer, if it resolved your problem.
– Robin
Nov 14 at 14:38
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
From what I understand you want to read the value from a text file and insert it into your XML configuration file.
MWE
$TextFile = "C:Folderfile.txt"
$XMLFile = "C:Folderconfig.xml"
$Value = Get-Content -Path $TextFile -Raw
[XML]$XML = Get-Content -Path $XMLFile
$XPath = "/configuration/appSettings/add[@value='5' and @key ='shopId']"
$Nodes = $XML.SelectNodes($xpath)
foreach ($Node in $Nodes) {
$Node.value = $Value
}
$XML.Save($XMLFile)
Cannot set "value" because only strings can be used as values to set XmlNode properties.
– ArtemKiryanov
Nov 14 at 13:24
Thank you. I fixed.
– ArtemKiryanov
Nov 14 at 13:53
@ArtemKiryanov Please mark this as the answer, if it resolved your problem.
– Robin
Nov 14 at 14:38
add a comment |
up vote
0
down vote
accepted
From what I understand you want to read the value from a text file and insert it into your XML configuration file.
MWE
$TextFile = "C:Folderfile.txt"
$XMLFile = "C:Folderconfig.xml"
$Value = Get-Content -Path $TextFile -Raw
[XML]$XML = Get-Content -Path $XMLFile
$XPath = "/configuration/appSettings/add[@value='5' and @key ='shopId']"
$Nodes = $XML.SelectNodes($xpath)
foreach ($Node in $Nodes) {
$Node.value = $Value
}
$XML.Save($XMLFile)
Cannot set "value" because only strings can be used as values to set XmlNode properties.
– ArtemKiryanov
Nov 14 at 13:24
Thank you. I fixed.
– ArtemKiryanov
Nov 14 at 13:53
@ArtemKiryanov Please mark this as the answer, if it resolved your problem.
– Robin
Nov 14 at 14:38
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
From what I understand you want to read the value from a text file and insert it into your XML configuration file.
MWE
$TextFile = "C:Folderfile.txt"
$XMLFile = "C:Folderconfig.xml"
$Value = Get-Content -Path $TextFile -Raw
[XML]$XML = Get-Content -Path $XMLFile
$XPath = "/configuration/appSettings/add[@value='5' and @key ='shopId']"
$Nodes = $XML.SelectNodes($xpath)
foreach ($Node in $Nodes) {
$Node.value = $Value
}
$XML.Save($XMLFile)
From what I understand you want to read the value from a text file and insert it into your XML configuration file.
MWE
$TextFile = "C:Folderfile.txt"
$XMLFile = "C:Folderconfig.xml"
$Value = Get-Content -Path $TextFile -Raw
[XML]$XML = Get-Content -Path $XMLFile
$XPath = "/configuration/appSettings/add[@value='5' and @key ='shopId']"
$Nodes = $XML.SelectNodes($xpath)
foreach ($Node in $Nodes) {
$Node.value = $Value
}
$XML.Save($XMLFile)
answered Nov 14 at 12:13
Florian
204110
204110
Cannot set "value" because only strings can be used as values to set XmlNode properties.
– ArtemKiryanov
Nov 14 at 13:24
Thank you. I fixed.
– ArtemKiryanov
Nov 14 at 13:53
@ArtemKiryanov Please mark this as the answer, if it resolved your problem.
– Robin
Nov 14 at 14:38
add a comment |
Cannot set "value" because only strings can be used as values to set XmlNode properties.
– ArtemKiryanov
Nov 14 at 13:24
Thank you. I fixed.
– ArtemKiryanov
Nov 14 at 13:53
@ArtemKiryanov Please mark this as the answer, if it resolved your problem.
– Robin
Nov 14 at 14:38
Cannot set "value" because only strings can be used as values to set XmlNode properties.
– ArtemKiryanov
Nov 14 at 13:24
Cannot set "value" because only strings can be used as values to set XmlNode properties.
– ArtemKiryanov
Nov 14 at 13:24
Thank you. I fixed.
– ArtemKiryanov
Nov 14 at 13:53
Thank you. I fixed.
– ArtemKiryanov
Nov 14 at 13:53
@ArtemKiryanov Please mark this as the answer, if it resolved your problem.
– Robin
Nov 14 at 14:38
@ArtemKiryanov Please mark this as the answer, if it resolved your problem.
– Robin
Nov 14 at 14:38
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53298924%2fhow-to-change-value-in-xml-from-another-file-with-powershell%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
It's not clear to me what problem you're facing. Does assigning the value not work? Are you asking how to read text from a file? Something else entirely? Are you getting errors? If so, what do they say?
– Ansgar Wiechers
Nov 14 at 11:18
Yes, i don't rewrite value from my text file. If i put $n.value = "1, or another", config file is update, but does not take the values from my text file.
– ArtemKiryanov
Nov 14 at 12:02
I would like to rewrite value from my text file.
– ArtemKiryanov
Nov 14 at 12:04
$n.value = "TEST"
->$n.value = Get-Content 'C:pathtofile.txt'
– Ansgar Wiechers
Nov 14 at 12:09