如何通过powershell从xml文件中获取一些内容?

发布于 2024-11-28 04:34:29 字数 767 浏览 1 评论 0原文

变量$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.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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

南渊 2024-12-05 04:34:29

由于 XML 数据的处理对于许多管理任务来说都是不可或缺的,因此 Powershell 有一个 XML 类型加速器。所以这也可以工作:

[xml]$xmldoc = $returnedxml
$xmldoc.releasepath

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
拥有 2024-12-05 04:34:29

我只是将其读入字符串...

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

然后使用 xpath 从中获取值...

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

XPath 对于从 XML 文件中提取内容非常强大,比使用 xmldoc 简单得多(编码方面)

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

世态炎凉 2024-12-05 04:34:29

您的变量 $Returnedxml 必须是带有绝对路径的文件名。但目前它是 System.Xml.XmlDocument 类的对象。

因此,更改您的变量,然后您就可以读取该文件。

或者,另一方面,如果您在 $Returnedxml 中已经有一个 XmlDocument 对象,那么您不必将其读入 $xmldoc 中。两人都来自同一个班级。只需使用 $Returnedxml

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

伴梦长久 2024-12-05 04:34:29

您使用了错误的负载;那个是用于文件的。使用 LoadXML 代替:

$xmldoc.LoadXml($Returnedxml)

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

$xmldoc.LoadXml($Returnedxml)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文