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?










share|improve this question




















  • 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















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?










share|improve this question




















  • 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













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?










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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














  • 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












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)





share|improve this answer





















  • 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











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















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

























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)





share|improve this answer





















  • 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















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)





share|improve this answer





















  • 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













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)





share|improve this answer












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)






share|improve this answer












share|improve this answer



share|improve this answer










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


















  • 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


















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

How to change which sound is reproduced for terminal bell?

Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

Can I use Tabulator js library in my java Spring + Thymeleaf project?