How can I accomplish something like this:
PS /home/nicholas/powershell>
PS /home/nicholas/powershell> $date=(Get-Date | ConvertTo-Xml)
PS /home/nicholas/powershell>
PS /home/nicholas/powershell> $date
xml Objects
--- -------
version="1.0" encoding="utf-8" Objects
PS /home/nicholas/powershell>
PS /home/nicholas/powershell> $date.OuterXml
<?xml version="1.0" encoding="utf-8"?><Objects><Object Type="System.DateTime">12/12/2020 2:43:46 AM</Object></Objects>
PS /home/nicholas/powershell>
but, instead, reading in a file?
how do I load/import/read/convert an xml
file using ConvertTo-Xml
for parsing with Select-Xml
using Xpath
?
PS /home/nicholas/powershell>
PS /home/nicholas/powershell> $xml=ConvertTo-Xml ./bookstore.xml
PS /home/nicholas/powershell>
PS /home/nicholas/powershell> $xml
xml Objects
--- -------
version="1.0" encoding="utf-8" Objects
PS /home/nicholas/powershell>
PS /home/nicholas/powershell> $xml.InnerXml
<?xml version="1.0" encoding="utf-8"?><Objects><Object Type="System.String">./bookstore.xml</Object></Objects>
PS /home/nicholas/powershell>
PS /home/nicholas/powershell> $xml.OuterXml
<?xml version="1.0" encoding="utf-8"?><Objects><Object Type="System.String">./bookstore.xml</Object></Objects>
PS /home/nicholas/powershell>
PS /home/nicholas/powershell> cat ./bookstore.xml
<?xml version="1.0"?>
<!-- A fragment of a book store inventory database -->
<bookstore xmlns:bk="urn:samples">
<book genre="novel" publicationdate="1997" bk:ISBN="1-861001-57-8">
<title>Pride And Prejudice</title>
<author>
<first-name>Jane</first-name>
<last-name>Austen</last-name>
</author>
<price>24.95</price>
</book>
<book genre="novel" publicationdate="1992" bk:ISBN="1-861002-30-1">
<title>The Handmaid's Tale</title>
<author>
<first-name>Margaret</first-name>
<last-name>Atwood</last-name>
</author>
<price>29.95</price>
</book>
<book genre="novel" publicationdate="1991" bk:ISBN="1-861001-57-6">
<title>Emma</title>
<author>
<first-name>Jane</first-name>
<last-name>Austen</last-name>
</author>
<price>19.95</price>
</book>
<book genre="novel" publicationdate="1982" bk:ISBN="1-861001-45-3">
<title>Sense and Sensibility</title>
<author>
<first-name>Jane</first-name>
<last-name>Austen</last-name>
</author>
<price>19.95</price>
</book>
</bookstore>
PS /home/nicholas/powershell>
Creating the xml
file within the REPL
console itself works as expected:
$xml = [xml]( Get-Content .\bookstore.xml -raw ); $xml | Select-Xml YourXPath
– BeneficGet-Content
and cast the result to XML. This is the single most common error I see when people read XML in PowerShell. Use$doc = New-Object xml; $doc.Load('path.to.xml');
. This deals with file encodings properly. UsingGet-Content
happily mangles your data. – ConsummationGet-Content -raw
? – BeneficGet-Content
. – BeneficGet-Content
pays attention to the BOM, so it will recognize UTF-16 unaided, but UTF-8 downloaded from the Internet usually has no BOM. AndGet-Content
will continue to butcher "foreign" single-byte encodings. Ultimately, it really is luck when it works. And it's entirely unnecessary to rely on luck with XML when transparent encoding detection is a fundamental part of the spec. – Consummation