Read XML with nested multiple elements/nodes

Welcome Forums General PowerShell Q&A Read XML with nested multiple elements/nodes

This topic contains 1 reply, has 2 voices, and was last updated by

 
Keymaster
2 years ago.

  • Author
    Posts
  • #58396

    Participant
    Points: 0
    Rank: Member

    I have this following XML file that is read with the Powershell script below. It reads all the elements to the screen except for the nested Filename:

    param([string] $xmlFilePath='P:\TestFileNamesT6.xml')
    [xml] $xmlContent = [xml] (Get-Content -Path $xmlFilePath)
    # the next line is missing in 99% of all examples I have seen
    [System.Xml.XmlElement] $root = $xmlContent.get_DocumentElement()
    # notice the corresponding change in the next line
    [System.Xml.XmlElement] $FileGroups = $root.FileGroups
    [System.Xml.XmlElement] $FileGroup = $null
    Clear-Host
    foreach($FileGroup in $Filegroups.ChildNodes)
    {
    [string] $FileGroupName = $FileGroup.FileGroupName
    [string] $DriveName = $FileGroup.DriveName
    [string] $Path = $FileGroup.Path
        foreach($Filename in $Filenames.ChildNodes)
        {
        [string] $Filename = $FileGroup.Filename
        Write-Host (“FileGroupName={0},DriveName={1},Path={2},Filename={3}” -f $FileGroupName,$DriveName,$Path,$Filename)
        }
        Write-Host (“FileGroupName={0},DriveName={1},Path={2},Filename={3}” -f $FileGroupName,$DriveName,$Path,$Filename)
    }
    

    Output:

    FileGroupName=Group1,DriveName=M:,Path=\\Host1.Share1.net\Group1,Filename=
    FileGroupName=Group2,DriveName=T:,Path=\\Host1.Share2.net\Group2,Filename=
    FileGroupName=Group3,DriveName=T:,Path=\\Host1.Share3.net\Group3,Filename=
    FileGroupName=Group4,DriveName=S:,Path=\\Host1.Share4.net\Group4,Filename=
    FileGroupName=Group5,DriveName=X:,Path=\\Host1.Share5.net\Group5,Filename=
    FileGroupName=Group6,DriveName=U:,Path=\\Host2.Share6.net\Group6,Filename=
    FileGroupName=Group7,DriveName=Y:,Path=\\Host2.Share7.net\Group7,Filename=
    FileGroupName=Group8,DriveName=Z:,Path=\\Host3.Share8.net\Group8,Filename=
    PS P:\>

     
    
    	
    		
    			Group1
    			M:
    			\\Host1.Share1.net\Group1
    			
    				File1.dat
    				File2.dat
    				File3.dat
    			
    		
    		
    			Group2
    			T:
    			\\Host1.Share2.net\Group2
    			
    				File4.csv
    				File5.csv
    				File6.csv
    			
    		
    		
    			Group3
    			T:
    			\\Host1.Share3.net\Group3
    			
    				File7.txt
    				File8.txt
    				File9.txt
    			
    		
    		
    			Group4
    			S:
    			\\Host1.Share4.net\Group4
    			
    				File10.dat
    				File10.dat.ok
    				File11.dat
    				File12.dat.ok
    			
    		
    		
    			Group5
    			X:
    			\\Host1.Share5.net\Group5
    			
    				File13.txt
    				File14.txt
    				File15.txt
    			
    		
    		
    			Group6
    			U:
    			\\Host2.Share6.net\Group6
    			
    				File16.csv
    				File17.csv
    				File18.csv
    			
    		
    		
    			Group7
    			Y:
    			\\Host2.Share7.net\Group7
    			
    				File19.dat
    				File19.dat.ok
    				File20.dat
    				File21.dat.ok
    			
    		
    		
    			Group8
    			Z:
    			\\Host3.Share8.net\Group8
    			
    				File22.dat
    				File22.dat.ok
    			
    		
    	
    
    
  • #58652

    Keymaster
    Points: 1,704
    Helping HandTeam Member
    Rank: Community Hero

    So, you'll need to put your XML into Gist on GitHub, and then paste the Gist URL here. Because XML looks just like HTML, browsers try to render it, which means we can't see the actual XML.

The topic ‘Read XML with nested multiple elements/nodes’ is closed to new replies.