PowerShell从XML属性编辑#text

时间:2012-04-26 00:22:24

标签: xml powershell

问题如下:

$xml = [xml](Get-Content $USSExternalContentTypeFile)   
$xml.Model.Properties.Property. ....
$xml.Save($USSExternalContentTypeFile)   

名称类型#text
---- ---- -----
Connect Integrated Security System.String textxtxtxtxtxtxtxt

<Property Name="Connect Integrated Security" Type="System.String">SSPI</Property>

帮助多个xml属性替换#text?

完成:     属性[2]。'#text'='foo'

2 个答案:

答案 0 :(得分:4)

尝试这种方式来访问#text属性:

PS> $xml = [xml]'<Property Name="Connect Integrated Security" Type="System.String">SSPI</Property> '
PS> $xml.Property

Name                                    Type                                    #text
----                                    ----                                    -----
Connect Integrated Security             System.String                           SSPI

PS> $xml.Property.'#text' = 'foo'

PS> $xml.Property

Name                                    Type                                    #text
----                                    ----                                    -----
Connect Integrated Security             System.String                           foo

答案 1 :(得分:0)

作为PowerShell的XML节点自动NoteProperties的替代方法,您还可以使用XPath来获取所需的节点。假设您希望所有属性节点的名称属性为“Connect Integrated Security”,您可以使用:

$nodes = $xml.SelectNodes("//Property[@Name='Connect Integrated Security']")
$nodes | % {
    $_."#text" = "SomeNewValue" # Changes SSPI to this.
}

如果你想坚持使用NoteProperties,这可能会有效:

$xml.Model.Properties.Property | % {
    $_."#text" = "SomeNewValue" # Changes SSPI to this.
}