检索变量的XML内容(Retrieve XML content of a variable)

编程入门 行业动态 更新时间:2024-10-24 06:25:56
检索变量的XML内容(Retrieve XML content of a variable)

我曾经有一个填充变量的XML文件([xml]$modificationData = Get-Content $modificationXmlFileName)但现在我无法存储文件,必须把它放到一个不同的数据文件中,我告诉的格式是这样做是:

xmldata = @' <bigSection> <smallSection> <changes> </changes> </smallSection> </bigSection> '@

我不确定Get-Content cmdlet现在是否可以正常工作,如果我将它连接到这个新变量并且我不完全确定我还能使用什么。

另外,我不知道@''@包围代码意味着什么。 我认为@符号用于数组,但我不认为它在这种情况下会这样做。

任何澄清都会有所帮助。

I used to have an XML file that was populating a variable ([xml]$modificationData = Get-Content $modificationXmlFileName) but now I am unable to store the file and must put it into a a different datafile, the format that I was told to do this in is:

xmldata = @' <bigSection> <smallSection> <changes> </changes> </smallSection> </bigSection> '@

I'm not sure if the Get-Content cmdlet would work now if I rout it to this new variable and I'm not entirely sure what else I could use.

Also, I don't know what the @''@ wrapping around the code means. I thought the @ symbol was used for arrays but I dont think that it does that in this case.

Any clarification would be helpful.

最满意答案

@'...'@语法称为here字符串或here文档。 这是一种指定字符串而不必转义字符的方法。

Get-Content用于读取文件。 当你默认调用它时,它会返回一个行数组,除非你用-Raw参数调用它(然后它将返回一个包含文件所有内容的大字符串)。

因此,在您的示例中, $xmldata与调用Get-Content $modificationXmlFileName -Raw 。

因为你只是想要转换为[xml] ,你可以这样做:

$xmldata = [xml]@' ... '@

The @'...'@ syntax is called a here string or a here document. It's a way of specifying a string without having to escape characters.

Get-Content is for reading files. When you call it by default, it returns an array of lines, unless you call it with the -Raw parameter (and then it would return one big string with all the content of the file).

So in your example, $xmldata is the same as if you had called Get-Content $modificationXmlFileName -Raw.

Since you're just trying to cast to [xml], you can just do that:

$xmldata = [xml]@' ... '@

更多推荐

本文发布于:2023-04-27 14:32:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1327188.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:变量   内容   XML   variable   Retrieve

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!