1

When creating a new XML file I have used

$settingNode = $xml.CreateElement('SettingNode')
$rootNode.AppendChild($settingNode) > $null

and it works, but when the InnerXML is blank the resulting node in the XML uses the shorthand approach

<SettingNode />

This works technically, but I am creating these XML files for people to cut and paste data into, so I would really prefer

<SettingNode></SettingNode>

so users have a place to paste without any extra work. That said, I can't find any flag for changing how the node is presented. Am I missing something, or am I limited to just the default representation and I'll need to copy seed XML files rather than create them programmatically?

Gordon
  • 6,257
  • 6
  • 36
  • 89

1 Answers1

1

I'd suggest adding a comment to the <SettingNode> element:

$settingNode = $xml.CreateElement('SettingNode')
$comment = $xml.CreateComment('INSERT DATA HERE')
$settingNode.AppendChild($comment) > $null
$rootNode.AppendChild($settingNode) > $null

which would give you a result like this:

<root>
  <SettingNode>
    <!--INSERT DATA HERE-->
  </SettingNode>
</root>
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
  • Hmm. For a number of reasons the single line approach is preferred, but I'll probably implement it both ways and use them both for a while. I already have some other seed files that get copied, so expanding on that is not particularly problematic. Bit of a bummer there isn't a parameter to force the output to any one of the three representations. – Gordon Aug 26 '18 at 11:30
  • 2
    I don't think you can do that using `XmlDocument` object methods. [Using an `XmlTextWriter`](https://stackoverflow.com/q/1176202/1630171) should work, though. – Ansgar Wiechers Aug 26 '18 at 11:36
  • Aha. Another way to do XML! I'll take a look at that too. Especially if it's faster I may want to refactor some other code that is annoyingly slow. – Gordon Aug 26 '18 at 12:40