Link to home
Start Free TrialLog in
Avatar of benpal2476
benpal2476Flag for United States of America

asked on

Powershell Parse XML and create multiple files

For instnace .....................

<Item>
   <value>a</value>
   <value>b</value>
</Item>

How do I extract contents of  <value> </value>  .InnerXml or "a" to a file and create a naming convention. then do the same for  <value> </value>  .InnerXml or "b"   and so on is this possible?

[xml]$xml = (get-content "path.xml")


Foreach ($value in $Item) {

write-host $value.InnerXml

$xml.save("C:\Scripts\new.xml*")
}
This doesent work.
ASKER CERTIFIED SOLUTION
Avatar of Dale Harris
Dale Harris
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of benpal2476

ASKER

[xml]$xml = (get-content "path.xml")


Foreach ($value in $Item) {

write-host $value.InnerXml

#$xml.save("C:\Scripts\new.xml*")
#without the "*" it writes to ("C:\Scripts\new.xml")

}

#but I want it to get InnerText for the <value> </value> there are multiple <value> within the xml I would like to get each <value> and output that data to a seperate file.
Try this instead:

foreach ($group in $xmlData.benchmark.group)
{
  $group.id
  $group.title
  $group.rule.id
}


Again, you're going to want to try to get a single value at the least to make sure it's working like you planned.

DH
Also, if you'd like to have me help a little more, you can post a portion of your XML code (so I can actually run it through and attempt to work with it myself) and I'll see if I can't cook something up.

DH
[xml]$xml = (get-content "C:\Users\user\Desktop\xml")

$xml.SelectNodes("/Item/Alias") |
Select * | Format-List

I created this it gives me a break down of Alias in the document, I know there are 40 Alias from doing a .count but now i'd like to save the innertext or inner xml of Alias to a seperate file.

Thanks for your time.
Using the following command will help:
$VariablewithStuffInside| get-member

This is the best way to see what kind of properties and method are in that object, just so you can see if you can hit a command to make it work.

I would love your xml file  you're actually importing with 40 aliases.  This way, I can just take a few minutes to look it over, generate the exact code you need, since you seem to having a difficulty getting the information you need out of this.

DH