0

Variable $returnedxml is a sql query result forming in xml. I need to get content 'releasepath'\\sharespace\test1\\10.0.1212.00from it

<ReleasePath>\\sharespace\test1\\10.0.1212.00</ReleasePath>

Here are my code:

 $xmldoc= new-object xml.xmldocument

 $xmldoc.load($Returnedxml)

 $xmldoc.releasepath

Here are the returned error alarm:

Exception calling "Load" with "1" argument(s): "Could not find file 'C:\Users\admin\System.Xml.XmlDocument'."
At D:\connecttods3andinvoke.ps1:47 char:14
+  $xmldoc.load <<<< ($Returnedxml)
+ CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException

I thought xml.xmldocument is a .net class, seems that I was wrong. So what can I do then?

4 Answers 4

4

Since the handling of XML data is so integral to so many management tasks, Powershell has an XML Type Accelerator. So this would work as well:

[xml]$xmldoc = $returnedxml
$xmldoc.releasepath
Sign up to request clarification or add additional context in comments.

2 Comments

According to the error message, it already is an XML document. So you can skip the first line.
But look at the file it is trying to find, 'C:\Users\admin\System.Xml.XmlDocument'. You can see there is a type name in the string, which is the default ToString() operation. That tells us that $Returnedxml is an XML document, because when converted to a string, you get the type name. Which is then treated as a relative path, so you get the result 'C:\Users\admin\System.Xml.XmlDocument'.
2

I just read it into a string ...

$file = [IO.File]::ReadAllText($filename)

then use xpath to get values out of it ...

$releasePath = $file | SelectXml "//ReleasePath"

XPath is really powerful for pulling things out of an XML file, much simpler (coding wise) than using xmldoc

Comments

1

Your variable $Returnedxml must be a file name with absolute path. But currently it is an object of class System.Xml.XmlDocument.

So change your variable and then you can read the file.

Or on the other hand if you already have an object of XmlDocument in $Returnedxml then you do not have to read it into $xmldoc. Both are from the same class. Just use $Returnedxml

Comments

0

You are using the wrong load; that one is for files. Use LoadXML instead:

$xmldoc.LoadXml($Returnedxml)

Comments

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.