Author Posts

September 27, 2017 at 5:27 pm

Hi,

I am trying to update a value to a key within an XML document.

Add-Type -AssemblyName System.Web
$myValue= ""

$xml.SelectSingleNode("//configuration).value = $myValue
$xml.save($file)

But my XML document is saved with the entity reference "&l t;" instead of the left tag symbol (<). I tried a number of different solutions to no avail. I cannot seem to escape it. My last possible solution was to add the HTMLDecode accelerator to hopefully keep the tags intact. Any suggestions to overcome this?

September 27, 2017 at 8:45 pm

Here is a snippet from some code I used to update xml keys. I can explain if you like but basically it just replaces two key values. You can ignore the switch:

$key1 = "reportPath"
$value1 = "C:\Folder Inventory Files\Folder Inventory Report.xlsx"
$key2 = "failedLoanPath"
$value2 = "C:\Folder Inventory Files\failed.txt"

New-Item "C:\Folder Inventory Files" -type directory

switch ($os) {

64-bit{

$config = "C:\Program Files (x86)\DocVelocity Folder Inventory Report\FolderInventoryReport.exe.config"
$doc = New-Object System.Xml.XmlDocument
$doc.Load($config)
$node = $doc.SelectSingleNode('configuration/appSettings/add[@key="' + $key1 + '"]')
$node.Attributes['value'].Value = $value1
$node = $doc.SelectSingleNode('configuration/appSettings/add[@key="' + $key2 + '"]')
$node.Attributes['value'].Value = $value2
$doc.Save($config)

}

32-bit {

$config2 = "C:\Program Files\DocVelocity Folder Inventory Report\FolderInventoryReport.exe.config"
$doc = New-Object System.Xml.XmlDocument
$doc.Load($config2)
$node = $doc.SelectSingleNode('configuration/appSettings/add[@key="' + $key1 + '"]')
$node.Attributes['value'].Value = $value1
$node = $doc.SelectSingleNode('configuration/appSettings/add[@key="' + $key2 + '"]')
$node.Attributes['value'].Value = $value2
$doc.Save($config2)

}

}

September 28, 2017 at 3:35 pm

Thanks, Jim. I completely understand your code. However, my issue is different and I was unable to explain myself properly due to trying to expressed XML left and right tags without escaping them properly to properly show my issue.

My issue is that when I try to update a value for a key attribute within an XML document, for unknown reason it is save the document using it's entity reference instead of the actual left/right tag symbol (left arrow or right arrow) instead I get (without spaces) & l t; and & r t;

Not sure why and trying to avoid that.

Any thoughts?