DaveWentzel.com            All Things Data

Working with XML

Querying

Updating/Adding Data

A
nother method to add Nodes

S
electNodes and Select-XML

X
PATH

 

Querying

Assume this basic setup:

cls
[xml]$xml = @"
<?xml version="1.0" encoding="utf-16"?>
<config>
  <Database1>Dave</Database1>
  <Winauth1>True</Winauth1>
  <force />
  <log>d:\Examiner Scripts\output\Examiner.log</log>
  <errlog>d:\Examiner Scripts\output\ExaminerErr.log</errlog>
  <options>
    <option name="KeepColumnOrder">False</option>
    <option name="IgnoreIndexes">False</option>
  </options>
  <excludes>
    <exclude type="table" expressiontype="wildcard">*SHSSFBuildManagers.*</exclude> 
    <exclude type="table" expressiontype="wildcard">*Backup*</exclude> 
<exclude type="table" expressiontype="wildcard">dbo.CUST*</exclude>
  </excludes>
  <includes>
    <include type="user" expressiontype="regexp">smssched</include>                   
<include type="user" expressiontype="regexp">CdTypes_CreCdValViews_InsUpdDelTrgr_Owner</include>  
  </includes>
</config>
"@

You can do a lot of cool querying...some examples:

 

 

Updating Data

this code:

 
# handle the "options" nodes

[xml]$xmlVar
= Add-ExaminerOptionsNodes $xmlVar

        

# handle the
"includes" parent node

$element
= $xmlVar.CreateElement("includes")

$xmlVar.config.AppendChild($element) |Out-null



# handle individual include nodes

$incelement
= $xmlVar.CreateElement("include")

$incelement.SetAttribute("type","dave")

$incelement.PSBase.InnerText = "wentzel"

$element.AppendChild($incelement) |Out-null


 
builds a structure that looks like this:
<config>
  <includes>
    <include type="dave">wentzel</include>
  </includes>
</config>

Another Method to Add Nodes

$something = [xml] '<blah></blah>'

$newnode
= $originalxml.ImportNode($something.blah, $true)

$parent.AppendChild($newnode)
 
SelectNodes and Select-XML
$xmldata.SelectNodes("something/something")

$xmldata.SelectNodes("something/something[1]")  #first entry

$xmldata.SelectNodes
("something/something[data = something]") #basic querying



$query
= "/something/name[string-length(something)<50]/something"

Select-XML –Xpath $query –Path
 
XPATH
XPATH queries are case sensitive.  
/ represents the root of the document
/Dave the node named Dave
/Dave/*/Wentzel navigates to the Wentzel node that is exactly one node under the Dave node
//Natalie finds all nodes named Natalie anywhere in the XML structure
@Name access the value of the attribute named Name
[] filtering.  Example:  //Node[@Attribute = 'Value']
 
XPATH queries are case sensitive.  
 
Syntax:
/  : represents the root of the document
/Dave : represents the node named Dave
/Dave/*/Wentzel  :  navigates to the Wentzel node that is exactly one node under the Dave node.  
//Natalie : finds all nodes named Natalie anywhere in the XML structure.
@Name : access the value of the attribute named Name
[]  :  filtering.  “//Node[@Attribute = ‘Value’]”
And/or/not()/=/!= : comparison operators.  
 
this code:
 
# handle the "options" nodes
[xml]$xmlVar = Add-ExaminerOptionsNodes $xmlVar 
        
    # handle the "includes" parent node
    $element = $xmlVar.CreateElement("includes")
    $xmlVar.config.AppendChild($element) |Out-Null
 
# handle individual include nodes
$incelement = $xmlVar.CreateElement("include")
$incelement.SetAttribute("type","dave")
$incelement.PSBase.InnerText = "wentzel"
$element.AppendChild($incelement) |Out-Null
 
builds a structure that looks like this:
 
<config>
  <includes>
    <include type="dave">wentzel</include>
  </includes>
</config>



PowerShell Home

Add new comment