4

I have a PowerShell script:

$xmlString="<root>
        <section>
            <node id='1'>AAA</node>
            <node id='2'>BBB</node>
            <node id='3'>CCC</node>
        </section>
    </root>"

$xml = New-Object -TypeName System.Xml.XmlDocument
$content = $xml.LoadXml($xmlString)

Value of $content is null

Inner Exception in $xml variable is <Error retrieving property - ArgumentException>

I have checked whether string starts with [System.Text.Encoding]::UTF8.GetPreamble() but it does not.

Can you please tell, what it is correct way to convert such string to XML?

5
  • 1
    You're just misunderstanding how LoadXML() works. The imported XML is in the variable $xml, not $content. The method doesn't return anything, so $content is empty. Commented Jan 11, 2019 at 11:41
  • @AnsgarWiechers I added your follow up to my answer :) Commented Jan 11, 2019 at 11:50
  • @Paxz Cheater ;þ Commented Jan 11, 2019 at 11:51
  • @AnsgarWiechers wanna make it an own answer? :D I can still roll back ;) Commented Jan 11, 2019 at 11:53
  • 1
    @Paxz Nah, I'm good. Commented Jan 11, 2019 at 11:55

2 Answers 2

6

You can directly cast the string to XmlDocument like this:

[xml]$xmlString="<root>
        <section>
            <node id='1'>AAA</node>
            <node id='2'>BBB</node>
            <node id='3'>CCC</node>
        </section>
    </root>"

If you want to keep the format of the variable, you can ofc just do it like this:

$xmlString="<root>
        <section>
            <node id='1'>AAA</node>
            <node id='2'>BBB</node>
            <node id='3'>CCC</node>
        </section>
    </root>"

[xml]$content = $xmlString

To follow up on @AnsgarWiechers comment, if you really want to use LoadXML, this is how it should look:

$xmlString=
"<root>
        <section>
            <node id='1'>AAA</node>
            <node id='2'>BBB</node>
            <node id='3'>CCC</node>
        </section>
</root>"

$xml = New-Object -TypeName System.Xml.XmlDocument
$xml.LoadXml($xmlString)

LoadXml will load a value from the given string to the $xml variable that calls the method.

It doesn't return any value, but saves it to $xml.

Sign up to request clarification or add additional context in comments.

Comments

1

ConvertFrom-Xml is what you need!

It's available from the PowerShell Gallery as part of Avande.CoolFunctions

$xmlString = @"
<root>
    <section>
        <node id='1'>AAA</node>
        <node id='2'>BBB</node>
        <node id='3'>CCC</node>
    </section>
</root>
"@

$xmlString | ConvertFrom-Xml

2 Comments

As far as I know, ConvertFrom-Xml is not a powershell built-in cmdlet. However, simply casting it with [xml] like @Paxz did is the way to go.
You're right @Theo ! I've added a link to the source of the CmdLet to my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.