How to add a child element for XML in PowerShell

后端 未结 1 1687
小蘑菇
小蘑菇 2020-12-16 09:19

I\'m trying to create a child XML element for this xml:




         


        
相关标签:
1条回答
  • 2020-12-16 09:28

    If you use dot notation to navigate an XML file (e.g. $doc.configuration), Powershell tries to be clever about what it returns.

    • If the target element is empty or only contains a single text node, PS will return a String.
    • If the target element contains child nodes other than text nodes, it will return an XmlElement.
    • If multiple target elements exist, it will return an Object[], where each individual array element is again subject to these rules, e.g. it will either be a String or an XmlElement depending on its contents.
    • If the target element does not exist, PS returns $null.

    In your case it's easy since you want to append nodes to the document element:

    $doc = New-Object System.Xml.XmlDocument
    $doc.Load($filePath)
    $child = $doc.CreateElement("newElement")
    $doc.DocumentElement.AppendChild($child)
    

    but you could use $doc.SelectNodes() or $doc.SelectSingleNode() to navigate around the XML document and always have a node/node list returned.


    One could argue about the sensibility of this behavior, but as a matter of fact it makes consuming (sanely structured) XML quite straight-forward - for example tasks such as reading values from a config file, or from an API response. That's the purpose of this simple syntax.

    It's not a good tool for creating XML, which is a more complex task. Using DOM API methods from the start is the better approach here.

    0 讨论(0)
提交回复
热议问题