Search code examples
xmlpowershellxpath

Why is the '#text' property not documented on the official Microsoft class definition page (XmlAttribute)?


Official MS Documentation for XmlAttribute

Value is documented. But not #text, despite it working just fine.

See these posts using it:

access #text property of XMLAttribute in powershell

PowerShell edit #text from XML attributes

And this guide:

https://www.red-gate.com/simple-talk/sysadmin/powershell/powershell-data-basics-xml/

This code works:

[SelectXmlInfo]$out = Select-Xml '//*[local-name()="svg"]/@viewBox' -Path $TheSVGFile
$out | ForEach-Object {
    $_.Node.GetType()
    $_.Node.'#text'
}

Output:

enter image description here

Where is the documentation located for this mysterious property/attribute?


Solution

  • #text as Property Name is a PowerShell thing using the .ToString() representation of an Xml Node as property name that's why you don't see documented as a property in the XmlNode Class or any of its descendants.

    Relevant documentation in this case is XmlNode.Name Property and XmlNodeType Enum.

    $xml = [xml]::new()
    $node = $xml.CreateTextNode('foo')
    [pscustomobject]@{
        $node.ToString() = $node.Value
    }
    
    # #text
    # -----
    # foo