forked from PowerShell/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXml.psm1
More file actions
101 lines (91 loc) · 2.2 KB
/
Xml.psm1
File metadata and controls
101 lines (91 loc) · 2.2 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
# Adds an attribute to a XmlElement
function New-XmlAttribute
{
param(
[Parameter(Mandatory)]
[string]$Name,
[Parameter(Mandatory)]
[object]$Value,
[Parameter(Mandatory)]
[System.Xml.XmlElement]$Element,
[Parameter(Mandatory)]
[System.Xml.XmlDocument]$XmlDoc
)
$attribute = $XmlDoc.CreateAttribute($Name)
$attribute.Value = $value
$null = $Element.Attributes.Append($attribute)
}
# Adds an XmlElement to an XmlNode
# Returns the new Element
function New-XmlElement
{
param(
[Parameter(Mandatory)]
[string]$LocalName,
[Parameter(Mandatory)]
[System.Xml.XmlDocument]$XmlDoc,
[Parameter(Mandatory)]
[System.Xml.XmlNode]$Node,
[Switch]$PassThru,
[string]$NamespaceUri
)
if($NamespaceUri)
{
$newElement = $XmlDoc.CreateElement($LocalName, $NamespaceUri)
}
else
{
$newElement = $XmlDoc.CreateElement($LocalName)
}
$null = $Node.AppendChild($newElement)
if($PassThru.IsPresent)
{
return $newElement
}
}
# Removes an XmlElement and its parent if it is empty
function Remove-XmlElement
{
param(
[Parameter(Mandatory)]
[System.Xml.XmlElement]$Element,
[Switch]$RemoveEmptyParents
)
$parentNode = $Element.ParentNode
$null = $parentNode.RemoveChild($Element)
if(!$parentNode.HasChildNodes -and $RemoveEmptyParent.IsPresent)
{
Remove-XmlElement -Element $parentNode -RemoveEmptyParents
}
}
# Get a node by XPath
# Returns null if the node is not found
function Get-XmlNodeByXPath
{
param(
[Parameter(Mandatory)]
[System.Xml.XmlDocument]
$XmlDoc,
[System.Xml.XmlNamespaceManager]
$XmlNsManager,
[Parameter(Mandatory)]
[string]
$XPath
)
if($XmlNsManager)
{
return $XmlDoc.SelectSingleNode($XPath,$XmlNsManager)
}
else
{
return $XmlDoc.SelectSingleNode($XPath)
}
}
Export-ModuleMember -Function @(
'Get-XmlNodeByXPath'
'Remove-XmlElement'
'New-XmlElement'
'New-XmlAttribute'
)