Reading XML data using Powershell foreach and assign to the values Variables

Roshan

I Am New to PowerShell. With some reference I wrote the some script for my below Scenario.

Suggest me if there is a better way to do this.

I have the following XML Data in a file "Test1.xml":

<TextValuess>
    <TextValues Name="Value1" Override="true" Type="String">
        <DEV>Source=DEV;Catalog=DEV_DMT;Integrated Security=SSPI;</DEV>
        <INT10>Source=LAB;Catalog=TST_INT10;Integrated Security=SSPI;</INT10>
        <INT>Source=LAB1;Catalog=TST_INT1;Integrated Security=SSPI;</INT>
        <INT2>Source=LAB10;Catalog=TST_INT12;Integrated Security=SSPI;</INT2>
    </TextValues>
    <TextValues Name="ENVIRONMENT" Override="true" Type="String">
        <DEV>DEV</DEV>
        <INT10>INT10</INT10>
        <INT>INT1</INT>
        <INT2>INT15</INT2>
    </TextValues>
</TextValuess>

I Was able to read the required data using the below PowerShell script. But I wasn't able to assign the values to PowerShell Variables.

My Powershell script is:

[xml]$XmlDocument = Get-Content D:\Roshan\Test1.xml
$child = 'INT10'
$XmlDocument.SelectNodes("//TextValues[./${child}]") | ForEach-Object {
    New-Object -Type PSObject -Property @{
        'Name'  = $_.Name
        'Value' = $_.$child
    }
}

I need some inputs/Suggestions how I can pass these XML Values to the Stored Procedure.

[xml]$XmlDocument = Get-Content D:\Roshan\Test1.xml
$child = 'INT10'

#Commands to connect with SQL Server ( I have done this)

$XmlDocument.SelectNodes("//TextValues[./${child}]") | ForEach-Object {
    New-Object -Type PSObject -Property @{
        $Name  = $_.Name   #Need to Assign Value to a Variable $Name
        $Value = $_.$child

      # I Want to call a stored procedure from XML here. 
    }
}
Theo

Also confused as to why you would need to create variables using the properties you have gathered, but here's something that might work for you then:

$child = 'INT10'
[xml]$XmlDocument = @"
<TextValuess>
    <TextValues Name="Value1" Override="true" Type="String">
        <DEV>Source=DEV;Catalog=DEV_DMT;Integrated Security=SSPI;</DEV>
        <INT10>Source=LAB;Catalog=TST_INT10;Integrated Security=SSPI;</INT10>
        <INT>Source=LAB1;Catalog=TST_INT1;Integrated Security=SSPI;</INT>
        <INT2>Source=LAB10;Catalog=TST_INT12;Integrated Security=SSPI;</INT2>
    </TextValues>
    <TextValues Name="ENVIRONMENT" Override="true" Type="String">
        <DEV>DEV</DEV>
        <INT10>INT10</INT10>
        <INT>INT1</INT>
        <INT2>INT15</INT2>
    </TextValues>
</TextValuess>
"@

$result = $XmlDocument.SelectNodes("//TextValues/$child") | ForEach-Object {
    New-Object -Type PSObject -Property @{
        # I don't know what you want in 'Name'.. Do you mean the 'Name' attribute from the parent node (TextValues) ?
        'ParentName' = $_.ParentNode.Name  # --> "Value1"
        'Name'       = $_.LocalName        # --> "INT10"
        'Value'      = $_.InnerText        # --> "Source=LAB;Catalog=TST_INT10;Integrated Security=SSPI;"
    }
}

$result now is an array with PSObjects:

ParentName  Name  Value                                                 
----------  ----  -----                                                 
Value1      INT10 Source=LAB;Catalog=TST_INT10;Integrated Security=SSPI;
ENVIRONMENT INT10 INT10

To use the various properties and put them in variables (although you could simply use them as-is..)

foreach ($item in $result) {
    $Name = $item.Name    # or $item.ParentName if that is what you are after
    $Value = $item.Value

    # do something with the variables
} 

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Assign values to C# variables by reading characters from a text file

Reading XML Data using SqlDataReader

Using dropDown menu to assign values to variables with knockout

Using variables in a for loop to assign legend values in pyplot

Reading and storing xml data in python variables

Why is my data frame loaded with blank values when reading XML using spark.xml? (Python/Databricks)

Reading and write XML content using Powershell

Reading XML attribute values using OPENXML

retrieve xml values using powershell

Get XML values using PowerShell

How to assign data values in array using ReactJS

Powershell getting same values when using if in foreach

C# Read xml file and assign values to variables

Reading data from XML using C#

Reading XML data using DOM in Java

Reading data from an XML File Using R

reading data using JAVA from XML files

assign multiple categorical values to series of data frame variables in r

Variables in Spark-sql Data-bricks to dynamically assign values

Assign values based on NA in other two variables of a data frame

Inconsistent assigning of values to Variables using tf.assign()

How to assign multiple variables different values using one function in Javascript

How to assign space-containing values to variables in bash using eval

Trying to assign 2 values or more to a dictionary key using variables?

Can't get XML values using foreach

Using "array_values" with "foreach" data?

Reading XML via PowerShell

how to assign variables to an object for submitting the Data using reactjs?

Assign multiple variables that reference each other, using data.table