Extract XML Element

Hi, I’m new to working with Powershell and would appreciate some help with extracting the element from an XML file and then 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 

Thanks

Use SelectSingleNode and if you want the XML specify OuterXML:

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

$xml.SelectSingleNode('//Generic/ValueId').OuterXml

Thanks for the quick reply, that works perfectly.

Nice job displaying the xml. Another way with select-xml:

(select-xml //ValueId source.xml).node.OuterXml
     
<ValueId>12345678</ValueId>