0

I'm trying to add a root comment to an XML file that doesn't have one. I'm close, but can't get it in the root/prolog area and instead can only get it injected after the first element.

(preloading [xml]$UpdatenuspecFile=somefile.nuspec)

What seems logical to me to work would be:

 $UpdatednuspecFile.'#comment' = "test"

But this results in:

Cannot set "#comment" because only strings can be used as values to set XmlNode properties.
At line:1 char:1
+ $UpdatednuspecFile.'#comment' = "test"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], SetValueException
    + FullyQualifiedErrorId : XmlNodeSetRestrictionsMoreThanOneNode

Some (no, a LOT) of Googlefu got me close using:

$XMLComment="This is my comment"
$UpdatednuspecFile = [xml]([System.IO.File]::ReadAllText(file.nuspec))
$Comment = $UpdatednuspecFile.CreateComment($XMLComment)
$UpdatednuspecFile.DocumentElement.InsertBefore($Comment,$UpatednuspecFile.DocumentElement)

I've tried many variations of above such as using InsertBefore and InserAfter for $UpatednuspecFile.DocumentElement, $UpatednuspecFile, $UpatednuspecFile.FirstNode, $UpatednuspecFile.xml, $UpatednuspecFile.package, etc etc.

And can't get it to show up immediately after the XML declaration; i.e. line 2.

Instead my comment always shows up inside the first element (package) at the beginning or at the bottom of it.

and displayable (depending on method above) via $UpdatednuspecFile.package.'#comment'

Expected result would be to insert and be displayable for $UpdatednuspecFile.'#comment" to show $XMLComment

bcurran3
  • 1
  • 3

0 Answers0