-
-
Notifications
You must be signed in to change notification settings - Fork 104
/
Copy pathAdd date to XML.ps1
66 lines (52 loc) · 1.54 KB
/
Add date to 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
<#
<configuration xmlns:patch="https://www.dotnet-helpers.com/xmlconfig/">
<dotnet-helpers-tutorials>
<tutorials name="tutorial:start">
<Topics hint="list">
<Topic>MVC</Topic>
<Topic>Jquery</Topic>
<Topic>OOPS</Topic>
</Topics>
</tutorials>
</dotnet-helpers-tutorials>
</configuration>
#>
<#
.SYNOPSIS
Write data to an xml file
.EXAMPLE
AddEntryInConfig -Selector "//tutorials[@name='tutorial:start']" -Path "C:\Desktop\test.xml" -NewNode "PowerShell"
.NOTES
https://dotnet-helpers.com/powershell/how-to-write-data-to-an-xml-file-using-powershell/
#>
function AddEntryInConfig
{
[CmdletBinding()]
param
(
[Parameter(Mandatory = $true)]
[string]
$Selector,
[Parameter(Mandatory = $true)]
[string]
$Path,
[Parameter(Mandatory = $true)]
[string]
$NewNode
)
# Get the content of the file using the path
[xml]$XmlDocument = Get-Content -Path $Path -Force
# Selecting all the childs by finding its parent with $Selector value
$events = $XmlDocument.SelectSingleNode($Selector)
# Assigning the chilld elements to variable
$topics = $events.Topics
# creating new Element with heading as "Topic"
$child = $XmlDocument.CreateElement("Topic")
# Assinging the value to the Element "Topic"
$child.InnerText = $NewNode
# The appendChild method is used to add the data in the newly created XmlElement to the XmlDocument.
$topics.AppendChild($child)
# Save in to the file
$XmlDocument.Save($Path)
}
# AddEntryInConfig -Selector "//tutorials[@name='tutorial:start']" -Path "C:\Desktop\test.xml" -NewNode "PowerShell"