none
replace string at particular line no in xml

    Question

  • $file="c:\bat.xml"
    $content = [system.io.file]::ReadAllText($file).replace($val,$newval)
    [system.IO.file]::writeAllText($file,$content)

    Above code replacing all the occurrences of $val with $newval and here I want to replace value on particular line only 

    Note : In case if I will use set-content or out-file I will get desired output but Git is not behaving well and convert xml into binary so I am strictly using same code above just want to apply on particular line in the whole file, I have tried below code but not getting success, without code change can anyone guide me to add where clause for particular line only to change the value (say line no 6 I took here)

    $file="c:\bat.xml"
    $content | where {$_.linenumber -eq 6}[system.io.file]::ReadAllText($file).replace($val,$newval)
    [system.IO.file]::writeAllText($file,$content)


    • Edited by Himanshu_Kulkarni Thursday, May 17, 2018 6:08 AM summary heading need more information
    Thursday, May 17, 2018 5:59 AM

Answers

  • cook.xml is bad xml.  You need to fix it.

    This line is bad:

    $node.'cook' = "chef"

    There is no such attribute as  that.

    This works fine.

    [xml]$xml = @'
    <?xml version="1.0" encoding="UTF-8"?>
    <par>
        <chal de="first" val="man"/>
        <chal de="sec" val="wman"/>
        <chal de="tim" val="cook"/>
        <chal de="cook" val="book"/>
    </par>
    '@
    $node =$xml.SelectSingleNode('//chal[@de="tim"]')
    $node
    $node.val = 'chef'
    $node.val


    \_(ツ)_/

    Friday, May 18, 2018 7:31 PM
    Moderator

All replies

  • XML does not have line numbers. 

    Load the file as XML and use a query to find the correct value to replace.

    [xml]$xml = Get-Content c:\bat.xml
     $node = $xml.SelectSingleNode('//tagname') 
    $node.'#text' = $newval
    $xml.Save('c:\bat.xml')


    \_(ツ)_/



    • Edited by jrvModerator Thursday, May 17, 2018 6:15 AM
    • Proposed as answer by BOfH_666 Thursday, May 17, 2018 6:27 AM
    Thursday, May 17, 2018 6:12 AM
    Moderator
  • actually this is not working (in git GUI the xml is convert into binary and not at all displaying any changes/file) 

    The xml looks like below structure and just change say val "cook" with "chef" only 

    <? xml version="1.0">
    <par>
    <chal de="first" val="man" />
    <chal de="sec" val="wman" />
    <chal de="tim" val="cook" />
    <chal de="cook" val="book" />
    </par>


    Friday, May 18, 2018 1:54 PM
  • XML is never converted into binary.  Perhaps you have other issues.

    To select the item you want just ask for it;

    $node =$xml.SelectSingleNode('//chal[@de="tim"]')
    $node.val
    $node.val = 'chef'


    \_(ツ)_/

    Friday, May 18, 2018 2:05 PM
    Moderator
  • The XML you posted has a malformed "XML" statement.

    It should be:

    <?xml version="1.0" encoding="UTF-8"?>


    \_(ツ)_/

    Friday, May 18, 2018 2:14 PM
    Moderator
  • cls
    cat C:\Temp\cook.xml
    $val="cook"
    [xml]$xml = Get-Content C:\Temp\cook.xml
    $node =$xml.SelectSingleNode('//chal[@de=$val]') 
    $node.value
    $node.Value = 'chef'
    $node.'cook' = "chef"
    $xml.Save('C:\Temp\cook.xml')
    
    

    This is giving an error, I have done few trials in $node but still parsing errors (cook.xml is above xml in the post )


    Friday, May 18, 2018 7:08 PM
  • cook.xml is bad xml.  You need to fix it.

    This line is bad:

    $node.'cook' = "chef"

    There is no such attribute as  that.

    This works fine.

    [xml]$xml = @'
    <?xml version="1.0" encoding="UTF-8"?>
    <par>
        <chal de="first" val="man"/>
        <chal de="sec" val="wman"/>
        <chal de="tim" val="cook"/>
        <chal de="cook" val="book"/>
    </par>
    '@
    $node =$xml.SelectSingleNode('//chal[@de="tim"]')
    $node
    $node.val = 'chef'
    $node.val


    \_(ツ)_/

    Friday, May 18, 2018 7:31 PM
    Moderator
  • [xml]$xml = gc C:\Temp\cook.xml
    $val="first"
    $node =$xml.SelectSingleNode('//chal[@de="'+$val+'"]')
    $node
    $newval='firstvalonly'
    $node.val=$newval
    $xml.Save('C:\Temp\cook.xml')
    
    

    I am apologies, you are awesome jrv

    Friday, May 18, 2018 7:54 PM
  • $val = 'first'
    $node =$xml.SelectSingleNode("//chal[@de='$val']")

    A cleaner way.

    <o:p></o:p>


    \_(ツ)_/

    Friday, May 18, 2018 8:22 PM
    Moderator