Extract XML Element

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Mutzig1

Thread Starter
Joined
Apr 6, 2020
Messages
1
Hi,
I need to extract the element from an XML file and output this to a txt file using powershell.

<?xml version="1.0" encoding="utf-8"?>
<Generic version="1.0.0">
<ValueId>12345678</ValueId>
<ServerIP>127.0.0.1</ServerIP>
</Generic>

I've been able to extract the value 12345678 to a file using this powershell;

$XML = [XML](Get-Content C:\source.xml)
$XML.Generic.ValueId | Out-File C:\output.txt

I'm struggling to get this exact <ValueId>12345678</ValueId> output to the output.txt
 
Joined
Apr 7, 2020
Messages
1
I don't know powershell but I am going to guess it has a "REPLACE" function like VB

In VB, it would be REPLACE("current string","value you are looking for","value you want to replace it with")

REPLACE("<ValueId>12345678</ValueId>","<ValueId>","") 'This would replace the first tag with nothing
REPLACE("<ValueId>12345678</ValueId>","</ValueId>","") 'This would replace the second tag with nothing
 

Xsage

Dave
Joined
Jan 8, 2016
Messages
314
Code:
$path = "C:\source.xml"
$string = Get-Content $path
$list = New-Object Collections.Generic.List[String]

foreach($line in $string){
    $list.Add($line)
}

Write-Host $list[2]
The above code should create the output you are looking for.

Here is some advice:

First of all when creating powershell scripts, its best to use "Windows Powershell ISE" its the powershell equivilent to an IDE for coding like visual studio and can be added as a feature under powershell. It now ships with most installs of windows and is a massive help.

As for what the script does:
$path is the variable where we define the location of the file.
$string is the variable where we store all of the contents of the file defined in $path.
$list is a where we create a List. A List is a .Net feature that is sort of like an array but provides a bunch of extra functions making it really easy to manipulate and search the list, its probably overkill here as an array would work, but my preference is to always use lists.

We then make a 'foreach' loop, that the script will reiterate through until it reaches the end of the file. Powershell is quite intelligent so if you pass it $string that contains lines with carage return line feeds, it knows that the other variable you define in the foreach variable will be a line.
Inside the loop, we add each line independently to the list.

Finally we can retrieve each line from the list independently. The indexing of lists start at '0' so the third item/line would be '2'. We can get this by doing $list[2].

You can change the script to do whatever you want with the output such as writing it to another file.
 
Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 807,865 other people just like you!

Latest posts

Staff online

Top