如何通过powershell从xml文件中获取一些内容?
变量$returnedxml
是一个以xml形式形成的sql查询结果。我需要从中获取内容'releasepath'\\sharespace\test1\\10.0.1212.00
这
<ReleasePath>\\sharespace\test1\\10.0.1212.00</ReleasePath>
是我的代码:
$xmldoc= new-object xml.xmldocument
$xmldoc.load($Returnedxml)
$xmldoc.releasepath
这是返回的错误警报:
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
我认为xml.xmldocument是一个.net类,看来我错了。那我能做什么呢?
Variable $returnedxml
is a sql query result forming in xml. I need to get content 'releasepath'\\sharespace\test1\\10.0.1212.00
from 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
由于 XML 数据的处理对于许多管理任务来说都是不可或缺的,因此 Powershell 有一个 XML 类型加速器。所以这也可以工作:
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:
我只是将其读入字符串...
然后使用 xpath 从中获取值...
XPath 对于从 XML 文件中提取内容非常强大,比使用 xmldoc 简单得多(编码方面)
I just read it into a string ...
then use xpath to get values out of it ...
XPath is really powerful for pulling things out of an XML file, much simpler (coding wise) than using xmldoc
您的变量
$Returnedxml
必须是带有绝对路径的文件名。但目前它是 System.Xml.XmlDocument 类的对象。因此,更改您的变量,然后您就可以读取该文件。
或者,另一方面,如果您在
$Returnedxml
中已经有一个 XmlDocument 对象,那么您不必将其读入$xmldoc
中。两人都来自同一个班级。只需使用 $ReturnedxmlYour variable
$Returnedxml
must be a file name with absolute path. But currently it is an object of classSystem.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您使用了错误的负载;那个是用于文件的。使用
LoadXML
代替:You are using the wrong load; that one is for files. Use
LoadXML
instead: