Click here to Skip to main content
Rate this: bad
good
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
0 Sergey Alexandrovich Kryukov 398
1 DamithSL 235
2 OriginalGriff 230
3 Zoltán Zörgő 149
4 Dave Kreskowiak 120
0 OriginalGriff 7,575
1 DamithSL 5,529
2 Sergey Alexandrovich Kryukov 5,279
3 Maciej Los 4,961
4 Kornfeld Eliyahu Peter 4,539


Advertise | Privacy | Mobile
Web01 | 2.8.141223.1 | Last Updated 10 Sep 2011
Copyright © CodeProject, 1999-2014
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