ARTICLE

How to add values to the string array from xml using Powershell

Posted by Vijai Anand Articles | SharePoint 2010 August 14, 2011
In this article we will be seeing how to add values to the string array from xml using Powershell.
Reader Level:


Description:

I have two xml files XML1 and XML2 which contains the following values

xml.png

I need to get the xml values in a string array which contains the values from XML1 where XML2 not contains XML1 values (i.e. XML1 has four values "A","B","C" & "D" and XML2 has two values "A" & "B". I need to get only "C" &"D" in the string array)

Steps Involved:

  1. Go to Start, Click on All Programs.
  2. Click on Microsoft SharePoint 2010 Products.
  3. Right click on SharePoint 2010 Management Shell, and then click on Run as administrator.

Using Powershell get all the values XML1 file:

Run the following script in the SharePoint 2010 Management Shell

$XML1path = "C:\Users\\Desktop\XML1.xml"
[String[]]$strXML1=@()
[xml]$xml = get-content $XML1path
$nodeXML1 = @()
foreach ($nodeXML1 in $xml.Inputs.XML1)
{
    [String[]]$strXML1=[String[]]$strXML1+$nodeXML1
}

Output:

Type $strXML1 in the SharePoint 2010 Management Shell, you will be getting the following values

A
B
C
D

Using Powershell get all the values XML2 file:

Run the following script in the SharePoint 2010 Management Shell

$XML2path = "C:\Users \Desktop\XML2.xml"
[String[]]$strXML2=@()
[xml]$xml = get-content $XML2path
$nodeXML2 = @()
foreach ($nodeXML2 in $xml.Inputs.XML2)
{
    [String[]]$strXML2=[String[]]$strXML2+$nodeXML2
}

Output:

Type $strXML2 in the SharePoint 2010 Management Shell, you will be getting the following values

A
B

Run the following script in the SharePoint 2010 Management Shell

[String[]] $final=@($strXML1 | where{$strXML2 -notcontains $_})

Output:

Type $final in the SharePoint 2010 Management Shell, you will be getting the following values

C
D
 

COMMENT USING

Trending up