none
What is best way to read xml using PowerShell?

    Question

  • Hello

    I am using following PowerShell script (this code will be used in compiled script) to read xml as mentioned below, please let me know is this correct approach or is there any other better approach?

    sample.xml

    <Location>
      <Site> 
        <City>Manassas</City> 
        <Street>20128 Portsmouth Street</Street> 
        <POBox>PBOX</POBox>
        <State>Manassas</State> 
        <Zip>10205</Zip>
        <Country>VA</Country>
        <OfficePhone>+1 123-456-7890</OfficePhone>
       </Site> 
    </Location>

    Script Code

    [xml]$AddressAttribute = Get-Content C:\scripts\sample.xml
    
    foreach( $Address in $AddressAttribute.location.Site){ 
       
           $NewStreet = $Address.Street
           $NewCity = $Address.city
           $NewPoBox = $Address.POBox
           $NewState = $Address.State
           $Newzip = $Address.zip
           $NewCountry = $Address.country
           $NewPhone = $Address.OfficePhone
       
    }
    write-host $NewStreet 
    write-host $NewCity  
    write-host $NewPoBox  
    write-host $NewState  
    write-host $Newzip  
    write-host $NewCountry 


    • Edited by Avian123 Thursday, April 20, 2017 12:46 PM
    Thursday, April 20, 2017 12:44 PM

All replies

  • I cannot speak about compiled script but this is a good approach to read XML file in PowerShell. It all depends on what is your purpose of reading the XML file and how do you want to use the data from it. Here is a good reference on this subject

    https://www.simple-talk.com/sysadmin/powershell/powershell-data-basics-xml/


    cheers..uc

    Thursday, April 20, 2017 1:58 PM
  • I would maybe just take it one more step, but kind of depends on what you are doing:
    [xml]$xml = Get-Content C:\temp\myxmlfile.xml;
    $reader = (New-Object System.Xml.XmlNodeReader $xml);
    now you can do stuff like $reader.SelectNodes("//Site[contains(Zip,""10205"")]")
    • Edited by sbogar Thursday, April 20, 2017 4:40 PM
    Thursday, April 20, 2017 4:38 PM
  • This converts flat XML to PowerShell objects:

    [xml]$xml = @'
    <Location>
    	<Site>
    		<City>Manassas</City>
    		<Street>20128 Portsmouth Street</Street>
    		<POBox>PBOX</POBox>
    		<State>Manassas</State>
    		<Zip>10205</Zip>
    		<Country>VA</Country>
    		<OfficePhone>+1 123-456-7890</OfficePhone>
    	</Site>
    	<Site>
    		<City>Manassas</City>
    		<Street>20128 Portsmouth Street</Street>
    		<POBox>PBOX</POBox>
    		<State>Manassas</State>
    		<Zip>10205</Zip>
    		<Country>VA</Country>
    		<OfficePhone>+1 123-456-7890</OfficePhone>
    	</Site>
    	<Site>
    	     <City>Manassas</City>
    	     <Street>20128 Portsmouth Street</Street>
    	     <POBox>PBOX</POBox>
    	     <State>Manassas</State>
    	     <Zip>10205</Zip>
    	     <Country>VA</Country>
    	     <OfficePhone>+1 123-456-7890</OfficePhone>
    	</Site>
    </Location>
    '@
    $xml.Location.Site | Select-Object city, Street, POBox, State, Zip, Country, OfficePhone


    \_(ツ)_/


    Thursday, April 20, 2017 6:31 PM
    Moderator