Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Saturday, August 4, 2012 9:35 AM
I am trying to get the childnodes by index in the code below. I am doing that because I don't want to get all the childnodes
[System.Xml.XmlDocument]$doc = New-Object System.Xml.XmlDocument
$doc.Load("C:\PowershellFiles\XmlLists.xml");
$node = $doc.SelectSingleNode("//Migration/SiteCollections/SiteCollection")
$siteUrl = $node.Attributes.GetNamedItem("SiteUrl").Value
$webUrl = $node.Attributes.GetNamedItem("WebUrl").Value
$site = Get-SPSite $siteUrl
$web = $site.OpenWeb($webUrl);
if ($doc.DocumentElement -ne $null)
{
$root = $doc.SelectNodes("//Migration/DestinationLists");
foreach ($node in $root)
{
if($node.HasChildNodes)
{
for ($i=1; $i -le $node.ChildNodes.Count-2; $i++)
{
$attrvalue = $node.ChildNodes[$i].Attributes[0].Value
Write-Host $attrvalue
}
}
}
}
However the code is giving me the following error:
Unable to index into an object of type System.Xml.XmlChildNodes.
At C:\PowershellFiles\ImportXmlToLists.ps1:36 char:55
+ $attrvalue = $node.ChildNodes[ <<<< $i].Attributes[0].Value
+ CategoryInfo : InvalidOperation: (1:Int32) [], RuntimeException
+ FullyQualifiedErrorId : CannotIndex
Any idea how can I loop through the child nodes by index and get the attributes?
Thanks a lot for your help.
All replies (3)
Saturday, August 4, 2012 9:59 PM âś…Answered
Try the Item() method: http://msdn.microsoft.com/en-us/library/system.xml.xmlnodelist.item.aspx
You can also enumerate the members of the object by piping the object returned by $node.ChildNodes into Get-Member.
Jose R. MCP
Code Samples
Monday, January 11, 2016 1:35 PM
I have also thsi Problem, 2 Month ago the Script works fine, but now it stops,
than i used this Syntax:
$attrvalue = $node.ChildNodes.item($i).Attributes[0].Value
SchoWe
Monday, January 11, 2016 1:49 PM
$xml.selectnodes('Migration/SiteCollections/SiteCollection')
Dan