Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 326 Vote(s) - 3.51 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to change the value of XML Element attribute using PowerShell?

#1
I am trying to access and change the particular attribute from XML tag

XML:

<office>
<staff branch="Hanover" Type="sales">
<employee>
<Name>Tobias Weltner</Name>
<function>management</function>
<age>39</age>
</employee>
<employee>
<Name>Cofi Heidecke</Name>
<function>security</function>
<age>4</age>
</employee>
</staff>
<staff branch="London" Type="Technology">
<employee>
<Name>XXXX</Name>
<function>gement</function>
<age>39</age>

From the above example I want to print <staff> branch attribute and then want to change it with one value such as New York in all the whole XML and using below code to do that

$xml=New-Object XML

$xml.Load("C:\FE6Work.xml")

$node=$xml.SelectNodes("/office/staff")

write-output $node.branch
$node.branch="New York"

But get an error stating can't find the element.

Can someone please help?
Reply

#2
You can access the attributes directly in the `[xml]` object like this:

# C:\temp> $xml = [xml](Get-Content C:\FE6Work.xml)
# C:\temp> $xml.office.staff

branch Type employee
------ ---- --------
Hanover sales {Tobias Weltner, Cofi Heidecke}
London Technology {XXXX, Cofi}

# C:\temp> $xml.office.staff | foreach{$_.branch = "New York"}
# C:\temp> $xml.office.staff

branch Type employee
------ ---- --------
New York sales {Tobias Weltner, Cofi Heidecke}
New York Technology {XXXX, Cofi}


Reply

#3
Try the following:

$nodes = $xml.SelectNodes("/office/staff");
foreach($node in $nodes) {
$node.SetAttribute("branch", "New York");
}

This will iterate through all nodes returned by SelectNodes() and modify each one.
Reply

#4
if we are taking attribute from console and changing its value ?

$path=Read-Host -Prompt 'Enter path of xml file'
[xml]$xmldata = get-content "$path"

$tag = Read-Host -Prompt 'Enter tag'
$value = Read-Host -Prompt 'Enter value'
$xmldata.InstallConfig.$tag="$value"
$xmldata.Save($path)
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through