-
Notifications
You must be signed in to change notification settings - Fork 0
/
026_XML.ps1
75 lines (59 loc) · 1.85 KB
/
026_XML.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#
# XML.ps1
#
#.\souce\menu.xml
#<?xml version="1.0" encoding="utf-8" standalone="yes"?>
#<breakfast_menu>
# <food>
# <name>Belgian Waffles</name>
# <price>$5.95</price>
# <description>
# two of our famous Belgian Waffles with plenty of real maple syrup
# </description>
# <calories>650</calories>
# </food>
# <food>
# <name>Strawberry Belgian Waffles</name>
# <price>$7.95</price>
# <description>
# light Belgian waffles covered with strawberries and whipped cream
# </description>
# <calories>900</calories>
# </food>
# <food>
# <name>Berry-Berry Belgian Waffles</name>
# <price>$8.95</price>
# <description>
# light Belgian waffles covered with an assortment of fresh berries and whipped cream
# </description>
# <calories>900</calories>
# </food>
#</breakfast_menu>
$filePath = '.\source\menu.xml'
$xml =[xml]( Get-Content $filePath -Encoding UTF8)
$xml.FirstChild.InnerText
$xml.breakfast_menu.food.Count
$foods = $xml.breakfast_menu.food
foreach($food in $foods)
{
$food.InnerXml
Write-Host
}
$xml.breakfast_menu.food | Sort-Object price | Select-Object price
#XML XPath Query
$path = "breakfast_menu/food/name"
$query = $xml.SelectNodes($path)
$query
#Modify XML
$xml.breakfast_menu.food[0].price
#$5.95
$xml.breakfast_menu.food[0].price = "`$6.95"
$xml.breakfast_menu.food[0].price
#Save to file
#$xml.Save()
$newfood = [xml] '<food><name>Berry-Berry Belgian Waffles</name><price>$8.95</price><description>light Belgian waffles covered with an assortment of fresh berries and whipped cream</description><calories>900</calories></food>'
$newfoodNode = $xml.ImportNode($newfood.food,$true)
$xml.breakfast_menu.AppendChild($newfoodNode)
#$xml.Save($filePath)
#Export output to xml
Get-ChildItem . | Export-Clixml D:\test.xml