35

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 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?

1

3 Answers 3

58

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.

Sign up to request clarification or add additional context in comments.

21 Comments

@user3759904: Did you type it as 'setAttribute' or 'SetAttribute' (it should be the latter, it's case-sensitive)?. If you did type it with the correct casing, what happens exactly (e.g. you get an error or the attribute doesn't change)?
Method invocation failed because [System.Xml.XPathNodeList] doesn't contain a method named 'SetAttribute'.
@user3759904: You're probably using a different PowerShell version, I have tested with 4.0 and it works flawlessly using your code and input. In any case, I have edited my answer with a longer version, that should work in earlier PowerShell versions as well.
Dude, this is awesome! I wrote this big fancy UI and thought I'd have to scrap the whole thing, because I couldn't figure out how to change XML values. +1, awesome, you rock.
@PeterK, I can't edit your post (too short edit), branch string is missing double quotes at the end.
|
15

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}                                                         

3 Comments

thanks for your help on this, getting a below error on xml.Save($path) any idea on this would be very helpful Multiple ambiguous overloads found for "Save" and the argument count: "1".
Ignore that its sorted
when i am updating file its writing blank values in the xml such as branch set to " " updated code is $folders=$xml.office.Staff | Select-Object{ $FolderNameInitial + $_.branch.Substring(4)} $nodes=$xml.SelectNodes("/office/staff") foreach($node in $nodes) { if($node -eq $null) { write-output "Atrribute/Property not found" } else { $node.SetAttribute("branch",$folders) } } $xml.Save($path)
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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.