简体   繁体   中英

Adding attributes to XML nodes with PowerShell adds namespaces

I'm trying to add some attributes to some newly created XML nodes using PowerShell but for some reason the attributes look like this:

<add d5p1:name="" d5p2:value="" xmlns:d5p2="noindex,nofollow" xmlns:d5p1="X-Robots-Tag" />

Any ideas why it is formatting the attributes like this with namespaces? My code is quite simple:

if ($node -ne $null) {
    $node.SetAttributeNode("name", "X-Robots-Tag")
    $node.SetAttributeNode("value", "noindex,nofollow")
}

When in doubt, read the documentation . The SetAttributeNode() method adds a namespaced attribute without value.

 public virtual XmlAttribute SetAttributeNode( string localName, string namespaceURI ) 

Parameters

localName
Type: System.String
The local name of the attribute.

namespaceURI
Type: System.String
The namespace URI of the attribute.

[...]

Remarks
The XmlAttribute does not have any children. Use Value to assign a text value to the attribute or use AppendChild (or a similar method) to add children to the attribute.

You're looking for the SetAttribute() method.

if ($node -ne $null) {
    $node.SetAttribute("name", "X-Robots-Tag")
    $node.SetAttribute("value", "noindex,nofollow")
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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