简体   繁体   English

PowerShell 点引用 XML 属性

[英]PowerShell dot reference XML attribute

Given a single XML node like this给定一个这样的 XML 节点

<MachineLogFileName overwrite="[bool]"></MachineLogFileName>

and assigned to a variable like this并分配给这样的变量

$validationNode = $validation.xml.SelectSingleNode("//Settings/$($node.name)")

where of course $node.name is 'MachineLogFileName' , and a variable $attribute.name of 'overwrite' , I want to be able to get the value of that attribute, but only as an attribute.当然$node.name'MachineLogFileName' ,变量$attribute.name'overwrite' ,我希望能够获得该属性的值,但只能作为一个属性。 I can use $validationNode.($attribute.name) , but IF there was a child node called <overwrite> and wasn't an attribute called 'overwrite' I would end up getting the inner text of the child node.我可以使用$validationNode.($attribute.name) ,但是如果有一个名为<overwrite>的子节点而不是一个名为'overwrite'的属性,我最终会得到子节点的内部文本。 So in a sense I want something like $validationNode.Attributes.($attribute.name) .所以从某种意义上说,我想要类似$validationNode.Attributes.($attribute.name) It seems like there is a collection there, with a single attribute in this case, but I can't seem to get the value this way.似乎那里有一个集合,在这种情况下只有一个属性,但我似乎无法通过这种方式获得该值。 Ultimately this needs to work with highly variable attribute and child node names, and I could just loop through all the attributes, but I need this in an IF so something like $validationNode.Attributes.($attribute.name) would be ideal.最终这需要使用高度可变的属性和子节点名称,我可以循环遍历所有属性,但我需要在 IF 中使用它,因此像$validationNode.Attributes.($attribute.name)这样的东西将是理想的。 And seems like it should be possible and I am just missing one aspect?似乎应该有可能,而我只是错过了一个方面? I hope.我希望。

I want to be able to get the value of that attribute, but only as an attribute.我希望能够获得该属性的值,但只能作为一个属性。 I can use $validationNode.($attribute.name) , but IF there was a child node called <overwrite> and wasn't an attribute called 'overwrite'我可以使用$validationNode.($attribute.name) ,但是如果有一个名为<overwrite>的子节点并且不是一个名为 'overwrite' 的属性

Use GetAttribute() :使用GetAttribute()

$validationNode = $validation.xml.SelectSingleNode("//Settings/$($node.name)")
$attributeValue = $validationNode.GetAttribute($attribute.name)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM