none
Power Shell with XML RRS feed

  • Question

  • I am having the below XML format. I want to read the XML from power shell as string values and display as an array. Can you please help me 

    XML -

    <steps>    
        <Step name="test" names="@number_of_steps" step_values="1,2,3"></Step>               
        <Step name="test" names="@number_of_steps" step_values="1,2,3" | names="@number_of_steps" step_values="4,5,6" </Step>
        <Step name="test" names="@number_of_steps" step_values="1,2,3" | names="@number_of_steps" step_values="4,5,6" | names="@number_of_steps" step_values="4,5,6" </Step>
    </steps>

    Script

    [xml]$xml = Get-Content c:\step_counts.xml

    How should I read the values in string and display as an array.

    • Moved by Bill_Stewart Monday, July 30, 2018 1:45 PM Unanswerable drive-by question
    Saturday, June 9, 2018 5:25 AM

All replies

  • The XML posted is not legal XML.  It is loaded with critical errors.

    This is how XML must be formed.

    [xml]$xml=@'
    <steps>    
         <Step name="test" names="number_of_steps" step_values="1,2,3"/>               
         <Step name="test" names="number_of_steps" step_values="1,2,3"/>
         <Step name="test" names="number_of_steps" step_values="1,2,3"/>
     </steps>
    '@
    $xml.steps.Step
    
    name names           step_values
    ---- -----           -----------
    test number_of_steps 1,2,3
    test number_of_steps 1,2,3
    test number_of_steps 1,2,3

    To learn how to use XML see: https://www.w3schools.com/xml/default.asp



    \_(ツ)_/

    Saturday, June 9, 2018 5:42 AM
  • Thanks for your response. I should have been little clear. I am calling an external xml file from Power Shell.

    testXML.xml

    <steps> <Step name="test" names="number_of_steps" step_values="1,2,3"/> <Step name="test" names="number_of_steps" step_values="1,2,3"/> <Step name="test" names="number_of_steps" step_values="1,2,3"/> </steps>

    From Power Shell, I am reading the XML and try to read the values based on parameter name and value.

    [xml]$testXML.xml = Get-Content C:\XMLSamples\testXML.xml

    $searchString = /steps

    $value= $testXML| Select-Xml -XPath $searchString

    and want to loop through $value and form an array list.

    Thanks in advance for your help.


    • Edited by prasadbvm Saturday, June 9, 2018 2:59 PM Changed the search string name
    Saturday, June 9, 2018 2:58 PM