locked
Question about xml and powershell : update data after finding by Select-XML RRS feed

  • Question

  • Hi everybody

    i have a question in xml with select-xml and path and update specific value

    The xml

    <?xml version="1.0"?>
    <!--State of wagos-->
    <WAGOS>
      <WAGO RNE="054FR09">
        <Information>
          <IP>X.X.X.X</IP>
          <Passerelle>X.X.X.X</Passerelle>
        </Information>
        <State>
          <Type>4</Type>
          <Recurrence>0</Recurrence>
        </State>
        <Place>
          <BAT>TEST</BAT>
        </Place>
        <Technician>
          <Name>Isse Tools</Name>
          <Telephone>0000000000</Telephone>
          <Mobile>
          </Mobile>
        </Technician>
      </WAGO>
      <WAGO RNE="054FR10">
        <Information>
          <IP>X.X.X.X</IP>
          <Passerelle>X.X.X.X</Passerelle>
        </Information>
        <State>
          <Type>0</Type>
          <Recurrence>0</Recurrence>
        </State>
        <Place>
          <BAT>TEST</BAT>
        </Place>
        <Technician>
          <Name>Essi Isse</Name>
          <Telephone>0000000000</Telephone>
          <Mobile>
          </Mobile>
        </Technician>
      </WAGO>
    </WAGOS>


    I want to search with RNE =>

    $xmlDoc = [System.Xml.XmlDocument](Get-Content $HISTORY_WAGOS -Encoding UTF8);

    $XMLPath = "/WAGOS/WAGO[@RNE='" + $line.RNE + "']" ;
    $CurrentNodeXML = $xmlDoc | Select-Xml -XPath $XMLPath | Select-Object -ExpandProperty "Node";

    It's OK to find if RNE exist or not

    IF not exist, i create entry 

    but how i update data with my var $CurrentNodeXML

    I can read the data by $CurrentNodeXML .Technician.Name but the property InnerText is not available

    I test this too => $current.Information.Item("IP").InnerText OK to read but impossible to write 

    $current.Information.Item("ip").InnerText = "test"
    La propriété « InnerText » est introuvable dans cet objet. Vérifiez qu’elle existe et qu’elle peut être définie.
    Au caractère Ligne:1 : 1
    + $current.Information.Item("ip").InnerText = "test"
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation : (:) [], RuntimeException
        + FullyQualifiedErrorId : PropertyNotFound

    It's possible to help how i update a innertext node to a specific node finding by Select-XML

    Thx

    Jerem

    • Moved by Bill_Stewart Tuesday, May 8, 2018 6:27 PM Unanswerable drive-by question
    Friday, March 9, 2018 12:10 PM

All replies

  • Please do not post colorized code.  Look at it.  It is broken and will not copy correctly and it is very hard to read in most browsers.

    It is very hard to understand your question.  Either get someone to help you with the English or post in a forum in your language.


    \_(ツ)_/


    • Edited by jrv Friday, March 9, 2018 5:30 PM
    Friday, March 9, 2018 5:29 PM
  • Here is the language specific forum: https://social.technet.microsoft.com/Forums/fr-fr/home?forum=ITCG

    \_(ツ)_/

    Friday, March 9, 2018 5:31 PM
  • My best guess at what you are asking:

    [xml]$xml = @'
    <?xml version="1.0"?>
    <!--State of wagos-->
    <WAGOS>
      <WAGO RNE="054FR09">
        <Information>
          <IP>X.X.X.X</IP>
          <Passerelle>X.X.X.X</Passerelle>
        </Information>
        <State>
          <Type>4</Type>
          <Recurrence>0</Recurrence>
        </State>
        <Place>
          <BAT>TEST</BAT>
        </Place>
        <Technician>
          <Name>Isse Tools</Name>
          <Telephone>0000000000</Telephone>
          <Mobile>
          </Mobile>
        </Technician>
      </WAGO>
      <WAGO RNE="054FR10">
        <Information>
          <IP>X.X.X.X</IP>
          <Passerelle>X.X.X.X</Passerelle>
        </Information>
        <State>
          <Type>0</Type>
          <Recurrence>0</Recurrence>
        </State>
        <Place>
          <BAT>TEST</BAT>
        </Place>
        <Technician>
          <Name>Essi Isse</Name>
          <Telephone>0000000000</Telephone>
          <Mobile>
          </Mobile>
        </Technician>
      </WAGO>
    </WAGOS>
    '@
    
    $node = $xml.SelectSingleNode('//WAGO[@RNE="054FR09"]')
    $node = $xml.SelectSingleNode('//WAGO[@RNE="054FR09"]')
    $node.Technician.Name
    

    To change:

    $node = $xml.SelectSingleNode('//WAGO[@RNE="054FR09"]')
    $node = $xml.SelectSingleNode('//WAGO[@RNE="054FR09"]')
    $node.Technician.Name = 'New tech name'
    


    \_(ツ)_/

    Friday, March 9, 2018 5:37 PM