By Disciple


2019-01-11 11:00:37 8 Comments

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?

2 comments

@Paxz 2019-01-11 11:08:34

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.

@gvee 2019-01-11 11:03:19

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

@Theo 2019-01-11 11:12:50

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.

@gvee 2019-01-11 11:19:35

You're right @Theo ! I've added a link to the source of the CmdLet to my answer.

Related Questions

Sponsored Content

48 Answered Questions

[SOLVED] How to replace all occurrences of a string in JavaScript

59 Answered Questions

[SOLVED] How do I read / convert an InputStream into a String in Java?

43 Answered Questions

[SOLVED] How do I convert a String to an int in Java?

81 Answered Questions

[SOLVED] How do I make the first letter of a string uppercase in JavaScript?

47 Answered Questions

24 Answered Questions

[SOLVED] Case insensitive 'Contains(string)'

19 Answered Questions

[SOLVED] Why is char[] preferred over String for passwords?

17 Answered Questions

[SOLVED] Does Python have a string 'contains' substring method?

57 Answered Questions

[SOLVED] What is the difference between String and string in C#?

76 Answered Questions

[SOLVED] How do I iterate over the words of a string?

  • 2008-10-25 08:58:21
  • Ashwin Nanjappa
  • 2086729 View
  • 2780 Score
  • 76 Answer
  • Tags:   c++ string split

Sponsored Content