Updating XML inserts entity reference instead of left tag symbol

Welcome Forums General PowerShell Q&A Updating XML inserts entity reference instead of left tag symbol

This topic contains 2 replies, has 2 voices, and was last updated by

 
Participant
1 year, 5 months ago.

  • Author
    Posts
  • #80720

    Participant
    Points: 45
    Rank: Member

    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?

  • #80750

    Participant
    Points: 0
    Rank: Member

    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)

    }

    }

  • #80867

    Participant
    Points: 45
    Rank: Member

    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?

The topic ‘Updating XML inserts entity reference instead of left tag symbol’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort