Last active
February 19, 2016 13:03
-
-
Save wgross/0439a771c49fc119789d to your computer and use it in GitHub Desktop.
Powershell functions to create some of the Linq2Xml classes like Xdocument, XElement, XNamespace, XName, XAttribute etc...
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function XName { | |
[CmdletBinding()] | |
param( | |
[Parameter(Mandatory,Position=0)] | |
[ValidateNotNullOrEmpty()] | |
[string]$LocalName | |
) | |
process { | |
[System.Xml.Linq.XName]$LocalName | |
} | |
} | |
function XAttribute { | |
[CmdletBinding(DefaultParameterSetName="createWithLocalName")] | |
param( | |
[Parameter(Mandatory,Position=0,ParameterSetName="createWithLocalName")] | |
[ValidateNotNullOrEmpty()] | |
[string]$LocalName, | |
[Parameter(Mandatory,Position=0,ParameterSetName="createWithXName")] | |
[ValidateNotNullOrEmpty()] | |
[System.Xml.Linq.XName]$Name, | |
[Parameter(Mandatory,Position=1)] | |
[ValidateNotNullOrEmpty()] | |
[System.Object]$Value | |
) | |
process { | |
switch($PSCmdlet.ParameterSetName) { | |
"createWithLocalName" { | |
New-Object System.Xml.Linq.XAttribute -ArgumentList @((XName -LocalName $LocalName),$Value) | |
} | |
"createWithXName" { | |
New-Object System.Xml.Linq.XAttribute -ArgumentList @($Name,$Value) | |
} | |
} | |
} | |
} | |
function XElement { | |
[CmdletBInding(DefaultParameterSetName="createWithLocalName")] | |
param( | |
[Parameter(Mandatory,Position=0,ParameterSetName="createWithLocalName")] | |
[ValidateNotNullOrEmpty()] | |
[string]$LocalName, | |
[Parameter(Mandatory,Position=0,ParameterSetName="createWithXName")] | |
[ValidateNotNullOrEmpty()] | |
[System.Xml.Linq.XName]$Name, | |
[Parameter(Mandatory=$false,Position=1,ParameterSetName="createWithLocalName")] | |
[Parameter(Mandatory=$false,Position=1,ParameterSetName="createWithXName")] | |
[ValidateNotNull()] | |
[scriptblock]$Content, | |
[Parameter(Mandatory=$false,Position=0,ParameterSetName="parse")] | |
[ValidateNotNull()] | |
[string]$Text, | |
[Parameter(Mandatory=$false,Position=0,ParameterSetName="load")] | |
[ValidateScript({Test-Path $_})] | |
$File | |
) | |
begin { | |
switch($PSCmdlet.ParameterSetName) { | |
"createWithLocalName" { | |
$XElement = New-Object System.Xml.Linq.XElement -ArgumentList @(XName -LocalName $LocalName) | |
} | |
"createWithXName" { | |
$XElement = New-Object System.Xml.Linq.XElement -ArgumentList $Name | |
} | |
"parse" { | |
$XElement = [System.Xml.Linq.XElement]::Parse($Text) | |
} | |
"load" { | |
$XElement = [System.Xml.Linq.XElement]::Load((Resolve-Path $File)) | |
} | |
} | |
} | |
process { | |
if($Content) { | |
([scriptblock]$Content).InvokeReturnAsIs() | foreach { $XElement.Add($_) } | |
} | |
} | |
end { | |
$XElement | |
} | |
} | |
function XDocument { | |
[CmdletBinding(DefaultParameterSetName="create")] | |
param( | |
[Parameter(Mandatory=$false,Position=0,ParameterSetName="create")] | |
[ValidateNotNull()] | |
[scriptblock]$Content, | |
[Parameter(Mandatory=$false,Position=0,ParameterSetName="parse")] | |
[ValidateNotNull()] | |
[string]$Text, | |
[Parameter(Mandatory=$false,Position=0,ParameterSetName="load")] | |
[ValidateScript({Test-Path $_})] | |
$Path | |
) | |
begin { | |
switch($PSCmdlet.ParameterSetName) { | |
"create" { | |
$XDocument = New-Object System.Xml.Linq.XDocument | |
} | |
"parse" { | |
$XDocument = [System.Xml.Linq.XDocument]::Parse($Text) | |
} | |
"load" { | |
$XDocument = [System.Xml.Linq.XDocument]::Load((Resolve-Path $Path)) | |
} | |
} | |
} | |
process { | |
if($PSCmdlet.ParameterSetName -eq "create") { | |
if($Content) { | |
([scriptblock]$Content).InvokeReturnAsIs() | foreach { $XDocument.Add($_) } | |
} | |
} | |
} | |
end { | |
$XDocument | |
} | |
} | |
function XComment { | |
[CmdletBinding()] | |
param( | |
[Parameter(Mandatory,Position=0)] | |
[ValidateNotNullOrEmpty()] | |
[string]$Comment | |
) | |
process { | |
[System.Xml.Linq.XComment]$Comment | |
} | |
} | |
function XCData { | |
[CmdletBinding()] | |
param( | |
[Parameter(Mandatory,Position=0)] | |
[ValidateNotNullOrEmpty()] | |
[string]$Value | |
) | |
process { | |
[System.Xml.Linq.XCData]$Value | |
} | |
} | |
function XText { | |
[CmdletBinding()] | |
param( | |
[Parameter(Mandatory,Position=0)] | |
[ValidateNotNullOrEmpty()] | |
[string]$Value | |
) | |
process { | |
[System.Xml.Linq.XText]$Value | |
} | |
} | |
function XNamespace { | |
[CmdletBinding()] | |
param( | |
[Parameter(Mandatory)] | |
[VAlidateNotNUll()] | |
[string]$Uri | |
) | |
process { | |
[System.Xml.Linq.XNamespace]($Uri) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment