top of page
Writer's pictureVijai Anand Ramalingam

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


Description:

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


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

  1. $XML1path = "C:\Users\\Desktop\XML1.xml"

  2. [String[]]$strXML1=@()

  3. [xml]$xml = get-content $XML1path

  4. $nodeXML1 = @()

  5. foreach ($nodeXML1 in $xml.Inputs.XML1)

  6. {

  7. [String[]]$strXML1=[String[]]$strXML1+$nodeXML

  8. }

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

  1. $XML2path = "C:\Users \Desktop\XML2.xml"

  2. [String[]]$strXML2=@()

  3. [xml]$xml = get-content $XML2path

  4. $nodeXML2 = @()

  5. foreach ($nodeXML2 in $xml.Inputs.XML2)

  6. {

  7. [String[]]$strXML2=[String[]]$strXML2+$nodeXML2

  8. }

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

0 comments

Comments


bottom of page