XML parsing changing values from CheckedListbox

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 2 years and 9 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
mar10c--
Posts: 59
Last visit: Mon May 22, 2023 6:53 am

XML parsing changing values from CheckedListbox

Post by mar10c-- »

<contents>
<content>
<name>John</name>
<active>0</active>
</content>
<content>
<name>Peter</name>
<active>0</active>
</content>
</contents>

[xml]$nameactives = Get-Content C:\test\names.xml
ForEach ($activename in $nameactives.contents.content.name)
{
$checkedlistbox1.Items.Add($activename)
}


foreach ($items in $checkedlistbox1.CheckedItems)
{
[xml]$nameactives = Get-Content C:\test\names.xml
ForEach ($activenames in $nameactives.contents.content)
{
?????????
}
$nameactives.Save("C:\test\names.xml")
}

I need to change the value of <active></active> to 1 or 0 based on name selected from $checkedlistbox1.CheckedItems
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: XML parsing changing values from CheckedListbox

Post by jvierra »

WHat is it that you are having an issue with? To alter an XML item just assign its #text property.

To find a specific item in an XML file use an XPath expression.
User avatar
mar10c--
Posts: 59
Last visit: Mon May 22, 2023 6:53 am

Re: XML parsing changing values from CheckedListbox

Post by mar10c-- »

my issue is $activenames.active = 1 below which changes all <active></active> instead of only the items in the $checkedlistbox1.CheckedItems

foreach ($items in $checkedlistbox1.CheckedItems)
{
[xml]$nameactives = Get-Content C:\test\names.xml
ForEach ($activenames in $nameactives.contents.content)
{
$activenames.active = 1
}
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: XML parsing changing values from CheckedListbox

Post by jvierra »

As I posted, you have to use an expath query to extract the exact item you want to update.
This topic is 2 years and 9 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked