Quantcast
Channel: XML, System.Xml, MSXML and XmlLite forum
Viewing all articles
Browse latest Browse all 935

Powershell - Read XML with multiple nodes - missing nested multiple nodes

$
0
0

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:\> 

<?xml version="1.0" ?> 
<Root>
<FileGroups>
<FileGroup>
<FileGroupName>Group1</FileGroupName>
<DriveName>M:</DriveName>
<Path>\\Host1.Share1.net\Group1</Path>
<Filenames>
<Filename>File1.dat</Filename>
<Filename>File2.dat</Filename>
<Filename>File3.dat</Filename>
</Filenames>
</FileGroup>
<FileGroup>
<FileGroupName>Group2</FileGroupName>
<DriveName>T:</DriveName>
<Path>\\Host1.Share2.net\Group2</Path>
<FileNames>
<Filename>File4.csv</Filename>
<Filename>File5.csv</Filename>
<Filename>File6.csv</Filename>
</FileNames>
</FileGroup>
<FileGroup>
<FileGroupName>Group3</FileGroupName>
<DriveName>T:</DriveName>
<Path>\\Host1.Share3.net\Group3</Path>
<FileNames>
<Filename>File7.txt</Filename>
<Filename>File8.txt</Filename>
<Filename>File9.txt</Filename>
</FileNames>
</FileGroup>
<FileGroup>
<FileGroupName>Group4</FileGroupName>
<DriveName>S:</DriveName>
<Path>\\Host1.Share4.net\Group4</Path>
<Filenames>
<Filename>File10.dat</Filename>
<Filename>File10.dat.ok</Filename>
<Filename>File11.dat</Filename>
<Filename>File12.dat.ok</Filename>
</Filenames>
</FileGroup>
<FileGroup>
<FileGroupName>Group5</FileGroupName>
<DriveName>X:</DriveName>
<Path>\\Host1.Share5.net\Group5</Path>
<Filenames>
<Filename>File13.txt</Filename>
<Filename>File14.txt</Filename>
<Filename>File15.txt</Filename>
</Filenames>
</FileGroup>
<FileGroup>
<FileGroupName>Group6</FileGroupName>
<DriveName>U:</DriveName>
<Path>\\Host2.Share6.net\Group6</Path>
<Filenames>
<Filename>File16.csv</Filename>
<Filename>File17.csv</Filename>
<Filename>File18.csv</Filename>
</Filenames>
</FileGroup>
<FileGroup>
<FileGroupName>Group7</FileGroupName>
<DriveName>Y:</DriveName>
<Path>\\Host2.Share7.net\Group7</Path>
<Filenames>
<Filename>File19.dat</Filename>
<Filename>File19.dat.ok</Filename>
<Filename>File20.dat</Filename>
<Filename>File21.dat.ok</Filename>
</Filenames>
</FileGroup>
<FileGroup>
<FileGroupName>Group8</FileGroupName>
<DriveName>Z:</DriveName>
<Path>\\Host3.Share8.net\Group8</Path>
<Filenames>
<Filename>File22.dat</Filename>
<Filename>File22.dat.ok</Filename>
</Filenames>
</FileGroup>
</FileGroups>
</Root>


Viewing all articles
Browse latest Browse all 935

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>