1

I have a list field that stores an XML string.

I am trying to get the XML contents and modify a particular tag in the XML like so:

foreach($item in $list.Items)
{
    if($item["XMLField"].Contains("NodeImLookingFor"))
    {
        $itemNum = $item["ID"]
        Write-Host ("{0} - {1}" -f $s, $itemNum)
        $a = $item["XMLField"]
        $a = $a.Replace("NodeImLookingFor", "ReplacementNode")
        $item["XMLField"] = $a
        $item.Update()
    }
}

The script above, however, deletes the contents in that field. Frustrated, I started over and repopulated the field with the XML. Once repopulated I tried the following:

foreach($item in $list.Items)
{
    if($item["XMLField"].Contains("NodeImLookingFor"))
    {
        Write-Host $item["XMLField"] #Displays the XML content in the cmd window
        $a = $item["XMLField"]
        Write-Host $a   #Does not display anything
    }
}

Now i'm confused should I just run the replace command straight on $item["XMLField"] instead of sticking its contents into the $a variable and editing it from there. My concern is I will delete the list field contents regardless. Please advise.

1 Answer 1

1

There is an XML type in PowerShell. Exploring XML Document by Using the [XML] Type Accelerator

Define $a as

[XML]$a = $item["XMLField"];

Now you would be able to lookup set the value of NodeImLookingFor as

$a.NodeImLookingFor = "New Value";

Regards

1
  • Thanks! But I am not trying to actually find and modify the value for NoteImLookingFor i'm trying to modify the name of the node itself. How do I do that? Commented May 22, 2015 at 17:14

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.