none
How to replace the Xml content values using shell script

    Question

  • Can anyone provide me the help to write the shell script to replace the values for below shown xml.

    Thank you.

    <?xml version='1.0' encoding='UTF-8'?>
    <preferences EXTERNAL_XML_VERSION='1.0'>
      <includes>
      </includes> 
      <root type='system'>  
           
            <node name='Controller' >
              <map>
             <entry key='MinimumLargeSize' value='0' />
               <entry key='XMLSchemaVersion' value='3' />
               <entry key='OutputXMLToDisk' value='false' />
             <entry key='OutputBeforeXML' value='false' />
             <entry key='OutputXMLDirectory' value='C:/test/fold1'/>
                  
       </map>
      </node>
     </root>
    </preferences>

    I would like to see as like below
    <?xml version='1.0' encoding='UTF-8'?>
    <preferences EXTERNAL_XML_VERSION='1.0'>
      <includes>
      </includes>
     
      <root type='system'>
            
            <node name='Controller' >
              <map>
             <entry key='MinimumLargeSize' value='1' />
          <entry key='XMLSchemaVersion' value='4' />
          <entry key='OutputXMLToDisk' value='true' />
          <entry key='OutputBeforeXML' value='true' />
          <entry key='OutputXMLDirectory' value='C:/test/fold2'/>
                  
       </map>
      </node>
     </root>
    </preferences>

    Wednesday, September 19, 2018 5:46 AM

All replies

  • I tried like the below. Able to retrieve the values from the xml for the given entries, but not able to replace the values. Can you please help me here. Thanks in advance

    $path = "C:\Desktop-18122018\2017\Automation-Powershell\example.cfg.xml"
    $xpath = "/preferences/root/node/map/entry[@key='MinimumLargeSize']"

    #Display the values

    [xml]$xml = Get-Content $path
    $MinimumLargeSize = $xml.preferences.root.node.map.entry | Where-Object {$_.key -eq 'MinimumLargeSize'} | Select-Object Value
    $XMLSchemaVersion = $xml.preferences.root.node.map.entry | Where-Object {$_.key -eq 'XMLSchemaVersion'} | Select-Object Value
    $OutputXMLToDisk = $xml.preferences.root.node.map.entry | Where-Object {$_.key -eq 'OutputXMLToDisk'} | Select-Object Value
    $OutputImageBeforeXML = $xml.preferences.root.node.map.entry | Where-Object {$_.key -eq 'OutputImageBeforeXML'} | Select-Object Value
    $OutputXMLDirectory = $xml.preferences.root.node.map.entry | Where-Object {$_.key -eq 'OutputXMLDirectory'} | Select-Object Value

    write  --------------------------------------------
    write MinimumLargeSize=$MinimumLargeSize
    write XMLSchemaVersion=$XMLSchemaVersion
    write OutputXMLToDisk=$OutputXMLToDisk
    write OutputImageBeforeXML=$OutputImageBeforeXML
    write OutputXMLDirectory=$OutputXMLDirectory

    $xml = [xml](Get-Content -Path $path)
    $NewMinimumLargeSize = $xml.SelectSingleNode($XPath).SetAttribute("0", "1")

     write New = $NewMinimumLargeSize

    Response:

    --------------------------------------------
    MinimumLargeSize=@{value=0}

    XMLSchemaVersion=@{value=3}

    OutputXMLToDisk=@{value=false}

    OutputXMLDirectory=@{value=C:/test/Receive}

    New

    =

    Thursday, September 20, 2018 4:45 PM
  • The "set" is wrong"

     $xml.SelectSingleNode($XPath).SetAttribute("value", "1")


    \_(ツ)_/

    Thursday, September 20, 2018 5:29 PM
  • Hello,

    Have tried with the same i.e. mentioned, still replace is not happening. New variable is returning null. Is any other way to replace the value. I will also try to find other ways.

    Thanks for your response and support.

    $path="C:\Desktop-18122018\2017\Automation-Powershell\example.cfg.xml"

    $xpath ="/preferences/root/node/map/entry[@key='MinimumLargeSize']"

    $xml=[xml](Get-Content-Path$path)

    $NewMinimumLargeSize =$xml.SelectSingleNode($xpath).SetAttribute("value","1")
     
    write New=$NewMinimumLargeSize

    New =



    • Edited by Subha29 Friday, September 21, 2018 12:20 PM
    Friday, September 21, 2018 6:15 AM
  • Please stop posting colorized code and post more that 2 or 3 lines with the code posting tool provided.

    $xml.SelectSingleNode($xpath).SetAttribute("value","1")
    $xml.SelectSingleNode($xpath)

    It will be changed.


    \_(ツ)_/



    • Edited by jrv Friday, September 21, 2018 6:26 AM
    Friday, September 21, 2018 6:22 AM
  • Thanks a lot. It worked. I will try to do for other values.
    Friday, September 21, 2018 12:21 PM
  • Thank you, mentioned script worked and able to see the change of values for required attributes. One thing I observed is all the entries and values in the xml file is getting updated  into double quotes where actually it should be in single quotes.

    Can anyone please help me how to update only the values without changing the quotes or anything else.

    E.g. Before update

    <entry key='MinimumLargeSize' value='0' />
               <entry key='XMLSchemaVersion' value='3' />
               <entry key='OutputXMLToDisk' value='false' />
             <entry key='OutputBeforeXML' value='false' />
             <entry key='OutputXMLDirectory' value='C:/test/fold1'/>

    After update

    <root type="system">
            
            <node name="Controller" >
              <map>
             <entry key="MinimumLargeSize" value="1" />
          <entry key="XMLSchemaVersion" value="4" />
          <entry key="OutputXMLToDisk" value="true" />
          <entry key="OutputBeforeXML" value="true" />
          <entry key="OutputXMLDirectory" value="C:/test/fold2"/>

    Thursday, November 8, 2018 10:04 AM
  • In XML single and double quotes are interchangeable.  You do not have to adjust this.  The default, industrywide, is double quotes.


    \_(ツ)_/

    Thursday, November 8, 2018 10:13 AM
  • Can you please give me some clue of getting in single quotes only, because this xml file need to be matched with the other xml files in which they are with values in single quotes.

    e.g. if in case of 100 entries in an xml file having in single quote, change of only 4 entry values changing the entire 100 entries into double quotes.

    Thursday, November 8, 2018 10:48 AM
  • The quote style does not matter.  All XML must allow both as quotes.  You do not need to change this back.  The industry default is double quotes.


    \_(ツ)_/

    Thursday, November 8, 2018 10:50 AM
  • # convert quotes to single quotes for any XML file
    $file = 'c:\temp\test.xml'
    $xml = Get-Content $file
    $xmlWriter = [System.Xml.XmlTextWriter]::new($file,[System.Text.Encoding]::UTF8)
    $xmlWriter.QuoteChar = "'"
    $xml.Save($xmlWriter)
    
    


    \_(ツ)_/

    • Marked as answer by Subha29 Thursday, November 8, 2018 4:45 PM
    • Unmarked as answer by Subha29 Thursday, November 8, 2018 4:46 PM
    Thursday, November 8, 2018 11:51 AM
  • Hello, thanks for the reply ... the above worked  changing to single but format got changed  like below

    <?xml version='1.0' encoding='utf-8'?><preferences EXTERNAL_XML_VERSION='1.0'><includes></includes><root type='system'><node name='Controller'><map><entry key='MinimumLargeSize' value='1' /><entry key='XMLSchemaVersion' value='4' /><entry key='OutputXMLToDisk' value='true' /><entry key='OutputImageBeforeXML' value='true' /><entry key='OutputXMLDirectory' value='C:/test/CSReceive' /></map></node></root></preferences>

    it should be like

    <root type='system'>
            
            <node name='Controller' >
              <map>
             <entry key='MinimumLargeSize' value='1' />
          <entry key='XMLSchemaVersion' value='4' />
          <entry key='OutputXMLToDisk' value='true' />
          <entry key='OutputBeforeXML' value='true' />
          <entry key='OutputXMLDirectory' value='C:/test/fold2'/>
                  
       </map>
      </node>
     </root>
    < /preferences>

    Thursday, November 8, 2018 4:49 PM