Home > database >  How just get Default and CurrentValue
How just get Default and CurrentValue

Time:09-16

I'm trying to read in an xml document, and just get Default and CurrentValue back where it's a certain name. I have it to this point, but not sure how to narrow it down further. Any ideas?

[xml]$doc = Get-Content  c:\Temp\TTTOrderSettings.xml

foreach($key in $doc.Configuration.Key)
#foreach($key in $doc.Configuration.Key.Key)
{
  if( ($key.Name -eq "Dilbert") )
  {
    if($key.Key.Name -eq "TTT")
    {
      #Key = $key.Name
      #Value = $key.Value
      echo $key.Key.Name
      echo $key.Key.Value
      #echo $key.Key.Name.Default
    }
  }

So the output looks like this:

Selection
TTT
Front
Preview
Priority
Finish
RedFaced

Name         #text       
----         -----       
Default      SelectManual
CurrentValue SelectManual
Default      Enhanced    
CurrentValue Enhanced    
Default      False       
CurrentValue False       
Default      True        
CurrentValue True        
Default      PriorityASAP
CurrentValue PriorityASAP
Default      Fabulous    
CurrentValue Fabulous    
Default      false       
CurrentValue False 

But I only need info from the Dilbert section, TTT, and only Default and Current names with values printed. How do I further narrow down what is printed?

This is what the file contents look like:

<?xml version="1.0" encoding="utf-16"?>
<Configuration>
    <Key Name="Archive">
        <Key Name="Selection">
            <Value Name="Default">SelectAll</Value>
            <Value Name="CurrentValue">SelectAll</Value>
        </Key>
        <Key Name="Front">
            <Value Name="Default">False</Value>
            <Value Name="CurrentValue">False</Value>
        </Key>
        <Key Name="Preview">
            <Value Name="Default">True</Value>
            <Value Name="CurrentValue">True</Value>
        </Key>
        <Key Name="Priority">
            <Value Name="Default">PriorityASAP</Value>
            <Value Name="CurrentValue">PriorityASAP</Value>
        </Key>
        <Key Name="Finish">
            <Value Name="Default">Fabulous</Value>
            <Value Name="CurrentValue">Fabulous</Value>
        </Key>
        <Key Name="RedFaced">
            <Value Name="Default">false</Value>
            <Value Name="CurrentValue">false</Value>
        </Key>
    </Key>
    <Key Name="Batch">
        <Key Name="TTT">
            <Value Name="Default">Off</Value>
            <Value Name="CurrentValue">Off</Value>
        </Key>
        <Key Name="Front">
            <Value Name="Default">False</Value>
            <Value Name="CurrentValue">False</Value>
        </Key>
        <Key Name="Preview">
            <Value Name="Default">True</Value>
            <Value Name="CurrentValue">True</Value>
        </Key>
        <Key Name="Priority">
            <Value Name="Default">PriorityASAP</Value>
            <Value Name="CurrentValue">PriorityASAP</Value>
        </Key>
        <Key Name="Finish">
            <Value Name="Default">Fabulous</Value>
            <Value Name="CurrentValue">Fabulous</Value>
        </Key>
        <Key Name="RedFaced">
            <Value Name="Default">false</Value>
            <Value Name="CurrentValue">false</Value>
        </Key>
        <Key Name="Scan">
            <Value Name="Default">Medium</Value>
            <Value Name="CurrentValue">Medium</Value>
        </Key>
        <Key Name="Type">
            <Value Name="Default">bed</Value>
            <Value Name="CurrentValue">bed</Value>
        </Key>
    </Key>
    <Key Name="Dilbert">
        <Key Name="Selection">
            <Value Name="Default">SelectManual</Value>
            <Value Name="CurrentValue">SelectManual</Value>
        </Key>
        <Key Name="TTT">
            <Value Name="Default">Enhanced</Value>
            <Value Name="CurrentValue">Enhanced</Value>
        </Key>
        <Key Name="Front">
            <Value Name="Default">False</Value>
            <Value Name="CurrentValue">False</Value>
        </Key>
        <Key Name="Preview">
            <Value Name="Default">True</Value>
            <Value Name="CurrentValue">True</Value>
        </Key>
        <Key Name="Priority">
            <Value Name="Default">PriorityASAP</Value>
            <Value Name="CurrentValue">PriorityASAP</Value>
        </Key>
        <Key Name="Finish">
            <Value Name="Default">Fabulous</Value>
            <Value Name="CurrentValue">Fabulous</Value>
        </Key>
        <Key Name="RedFaced">
            <Value Name="Default">false</Value>
            <Value Name="CurrentValue">False</Value>
        </Key>
    </Key>
    <Key Name="Flimsy">
        <Key Name="Softy">
            <Value Name="Default">true</Value>
            <Value Name="CurrentValue">True</Value>
        </Key>
        <Key Name="OrderType">
            <Value Name="Default">OrderTypeNew</Value>
            <Value Name="CurrentValue">OrderTypeNew</Value>
        </Key>
        <Key Name="Resolution">
            <Value Name="Default">Medium</Value>
            <Value Name="CurrentValue">Medium</Value>
        </Key>
        <Key Name="Boastable">
            <Value Name="Default">false</Value>
            <Value Name="CurrentValue">False</Value>
        </Key>
        <Key Name="Type">
            <Value Name="Default">Full</Value>
            <Value Name="CurrentValue">Full</Value>
        </Key>
        <Key Name="TTT">
            <Value Name="Default">Enhanced</Value>
            <Value Name="CurrentValue">Enhanced</Value>
        </Key>
        <Key Name="Front">
            <Value Name="Default">False</Value>
            <Value Name="CurrentValue">False</Value>
        </Key>
        <Key Name="Preview">
            <Value Name="Default">True</Value>
            <Value Name="CurrentValue">True</Value>
        </Key>
        <Key Name="Priority">
            <Value Name="Default">PriorityASAP</Value>
            <Value Name="CurrentValue">PriorityASAP</Value>
        </Key>
        <Key Name="Finish">
            <Value Name="Default">Fabulous</Value>
            <Value Name="CurrentValue">Fabulous</Value>
        </Key>
        <Key Name="RedFaced">
            <Value Name="Default">false</Value>
            <Value Name="CurrentValue">false</Value>
        </Key>
        <Key Name="Included">
            <Value Name="Default">true</Value>
            <Value Name="CurrentValue">true</Value>
        </Key>
    </Key>

</Configuration>

find xml

CodePudding user response:

Based on your use of PowerShell's adaption of the XML DOM, do the following:

$doc.Configuration.Key.
  Where({ $_.Name -eq 'Dilbert' }).
  Key.
  Where({ $_.Name -eq 'TTT' }).Value

Display output:

Name         #text
----         -----
Default      Enhanced
CurrentValue Enhanced

A potentially superior alternative, as TheMadTechnician points out, is to use an XPath query with the Select-Xml cmdlet:

$xmlFile = 'c:\Temp\TTTOrderSettings.xml'
(
  Select-Xml `
    -LiteralPath $xmlFile `
    -XPath '/Configuration/Key[@Name="Dilbert"]/Key[@Name="TTT"]/Value'
).Node

In both cases, [System.Xml.XmlElement] instances are output; use .Name to access their name, and - given that they only have text content - .InnerText to access their value.

  • Related