1

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")
}

1 Answer 1

2

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")
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.