I have an XML file and would like to use PowerShell to search for the node based on a variable and then set the attribute as another variable which I can then use.
我有一个XML文件,希望使用PowerShell基于一个变量搜索节点,然后将属性设置为另一个变量,然后我可以使用它。
My XML is saved at C:\example.xml and the code is below:
我的XML保存在C:\示例中。xml,代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<Portal>
<A23062013 Current="13" lastweek="12" />
<A24062013 Current="13" lastweek="12" />
</Portal>
The content of the XML nodes within the Portal node are actually dates in the format of ddMMyyy and these are prefixed with an A as above.
门户节点中的XML节点的内容实际上是以ddMMyyy格式的日期,这些日期以上面的A作为前缀。
My Powershell code is below:
我的Powershell代码如下:
$date = (Get-Date).AddDays(-1).ToString('ddMMyyyy')
$pre = "A"
$completedate = "$pre$date"
[xml] $file = get-content "C:\example.xml"
$xmlValuePrevious = $file.SelectNodes ('$completedate') | select lastweek
$xmlValueCurrent = $file.SelectNodes ('$completedate') | select Current
Write-Host "$xmlValuePrevious $xmlValueCurrent"
It currently throws up errors for lines 5 and 6 which say:
它目前为第5和第6行抛出错误,它们说:
Unexpected token '(' in expression or statement.
意外令牌'('在表达式或语句中。
Probably something easy that I'm missing as I'm new to PS, but I've searched for ages and cant find anything specific about this issue.
可能是我刚接触PS的时候错过的一些简单的东西,但是我已经找了很久,找不到关于这个问题的任何细节。
Thanks, Ad
谢谢,广告
2 个解决方案
#1
4
There are several issues with your code:
您的代码有几个问题:
- Remove the space between
SelectNodes
and the opening parenthesis. - 删除SelectNodes和展开括号之间的空间。
- Use double quotes if you want to expand variables inside a string. Or remove the quotes entirely if you want to pass only the variable.
- 如果要在字符串中展开变量,请使用双引号。或者完全删除引号,如果您只想传递变量。
- To select a node anywhere in the XML tree you need to prepend the node name with
//
. Using just the node name in an XPath expression will select only nodes with that name in the current node. - 要在XML树的任何地方选择节点,需要在节点名前面加上//。仅使用XPath表达式中的节点名,将只在当前节点中选择具有该名称的节点。
Change this:
改变:
$xmlValuePrevious = $file.SelectNodes ('$completedate') | select lastweek
$xmlValueCurrent = $file.SelectNodes ('$completedate') | select Current
into this:
到这个:
$xmlValuePrevious = $file.SelectNodes("//$completedate") | select lastweek
$xmlValueCurrent = $file.SelectNodes("//$completedate") | select Current
#2
3
Since Ansgar allready explained what the problem with your code is i wont go into that.
因为Ansgar allready解释了你的代码的问题,我就不深入了。
For accessing xml structures though you could also use the dot notation (which is a bit easier for beginners):
对于访问xml结构,您还可以使用点表示法(这对初学者来说更容易一些):
$xmlValuePrevious = $file.Portal.$completedate.lastweek
$xmlValueCurrent = $file.Portal.$completedate.current
#1
4
There are several issues with your code:
您的代码有几个问题:
- Remove the space between
SelectNodes
and the opening parenthesis. - 删除SelectNodes和展开括号之间的空间。
- Use double quotes if you want to expand variables inside a string. Or remove the quotes entirely if you want to pass only the variable.
- 如果要在字符串中展开变量,请使用双引号。或者完全删除引号,如果您只想传递变量。
- To select a node anywhere in the XML tree you need to prepend the node name with
//
. Using just the node name in an XPath expression will select only nodes with that name in the current node. - 要在XML树的任何地方选择节点,需要在节点名前面加上//。仅使用XPath表达式中的节点名,将只在当前节点中选择具有该名称的节点。
Change this:
改变:
$xmlValuePrevious = $file.SelectNodes ('$completedate') | select lastweek
$xmlValueCurrent = $file.SelectNodes ('$completedate') | select Current
into this:
到这个:
$xmlValuePrevious = $file.SelectNodes("//$completedate") | select lastweek
$xmlValueCurrent = $file.SelectNodes("//$completedate") | select Current
#2
3
Since Ansgar allready explained what the problem with your code is i wont go into that.
因为Ansgar allready解释了你的代码的问题,我就不深入了。
For accessing xml structures though you could also use the dot notation (which is a bit easier for beginners):
对于访问xml结构,您还可以使用点表示法(这对初学者来说更容易一些):
$xmlValuePrevious = $file.Portal.$completedate.lastweek
$xmlValueCurrent = $file.Portal.$completedate.current