I have defined an XML string using
$str = "<a><b></b></a>"
Now I want to load it into a [xml] variable to be able to manipulate it's nodes etc. The row below results in an error...
$str = [xml]"<a><b></b></a>"
How do I do this?
One way to read an XML document in PowerShell is to typecast a variable to the type [xml]. To create this variable, we can use the Get-Content cmdlet to read all of the text in an XML document. To typecast the output of Get-Content we can simply prepend the text [xml] before the variable.
Another way to use PowerShell to parse XML is to convert that XML to objects. The easiest way to do this is with the [xml] type accelerator. By prefixing the variable names with [xml] , PowerShell converts the original plain text XML into objects you can then work with.
To update the specific XML node using PowerShell, we first need to select that node with the attribute with SelectSingleNode() method. We have below the XML file from the link stored in SampleXml. XML on C:\Temp location. The above commands will load the XML file and select node with attribute value 'bk102'.
The code in your question seems to work for me. I just added test
inside my string to show I can access the value, but it should work without it, too.
Full Image
Casting a string also worked for me:
Full Image
...and I ran it with your string and it worked fine... Full Image
Try this
$xmlcontent = variable which holds xml data in string
$xml = New-Object -TypeName System.Xml.XmlDocument
$xml.LoadXml($xmlcontent)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With