Click here to Skip to main content
12,072,809 members (72,972 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: PowerShell
here is the hierarchy that need to create in XML
Example:
MyFile-
<Meta>
 <Image>
   <PNG>
       <URL>
   <PNG>
       <URL>
   <PNG>
       <URL>
   <PNG>
       <URL>
   <PNG>
       <URL>

my current implementation:
[xml] $file = Get-content C:\MyFile.xml
$Parent =$file.Meta
$Child = $file.CreateElement("Image",$Parent.NamespaceURI)
$Parent.AppendChild($Child)
$file.Save("C:\MyFile.xml")
 
for($j=0; $j -lt 10; $j++){
	$ParentNode =$file.Meta["Image"]
	$ChildNode = $file.CreateElement("PNG",$Parent.NamespaceURI)
	$ParentNode.AppendChild($ChildNode)
	$file.Save("C:\MyFile.xml")
   
	$ParentSubNode =$file.Meta.Image["PNG"]
	$SubChildNode = $metafile.CreateElement("URL",$ParentNode.NamespaceURI)
	$ParentSubNode.AppendChild($SubChildNode)
	$file.Save("C:\MyFile.xml")
    }


The above code is crating the xml as;
<Meta>
 <Image>
   <PNG>
       <URL>
       <URL>
       <URL>
       <URL>
   <PNG>
   <PNG>
   <PNG>

*the node names should not be changed with suffix 0,1,2..etc

Can some one pls help me to solve this issue?
Posted 9-Sep-11 22:38pm
Edited 10-Sep-11 8:53am
v7
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

It would appear that all your SubChildNodes are being added to the first PNG node, rather than each one in turn. I have not been able to test this, but perhaps something like:
$ParentNode =$file.Meta["Image"]
for($j=0; $j -lt 10; $j++){
    $ChildNode = $file.CreateElement("PNG",$Parent.NamespaceURI)
    $ParentNode.AppendChild($ChildNode)
    $SubChildNode = $metafile.CreateElement("URL",$ParentNode.NamespaceURI)
    $ChildNode.AppendChild($SubChildNode)
}
$metafile.Save("MyFile.xml")
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

Appreciated...Thanks a lot Richard, It works as expected.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web04 | 2.8.160208.1 | Last Updated 10 Sep 2011
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100