Guest User

Untitled

a guest
Nov 17th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.97 KB | None | 0 0
  1. # Reads xml file of persons with name, age and siblings
  2. # Outputs each person + siblings as text
  3. # Tested with PS v5.1
  4. # November 2018, Countryen
  5. # ==================
  6.  
  7. # Configuration
  8. $CFG = @{
  9. CRLF = [Environment]::NewLine
  10. InputPath = ".\data.xml"
  11. OutputPath = ".\data.txt"
  12. }
  13.  
  14. # Read content of file, convert to XML
  15. $xml = [Xml](Get-Content $CFG.InputPath)
  16.  
  17. # Find all elements (sequence)
  18. $elements = $xml.Root.Person
  19.  
  20. # Filter (many elements
  21. $result = $elements | % { $_.Name, "----", ($_.Siblings.Sibling | % { "> " + $_.Name }), $CFG.CRLF }
  22.  
  23. # Output result to file
  24. $result | Out-File $CFG.OutputPath
  25.  
  26. ### Example Output:
  27. # Mike
  28. # -----
  29. # > June
  30. # > July
  31. #
  32. # Christian
  33. # >
  34. #
  35.  
  36. ### Example XML:
  37. # <Root>
  38. # <Person>
  39. # <Name>Mike</Name>
  40. # <Age>22</Age>
  41. # <Siblings>
  42. # <Sibling>
  43. # <Name>June</Name>
  44. # </Sibling>
  45. # <Sibling>
  46. # <Name>July</Name>
  47. # </Sibling>
  48. # </Siblings>
  49. # </Person>
  50. # <Person>
  51. # <Name>Christian</Name>
  52. # <Age>16</Age>
  53. # </Person>
  54. # </Root>
Add Comment
Please, Sign In to add comment