1

I'm using powershell to update the Version node in a wpf csproj file. Everytime I add the version node (never understood I have to when it's already there), suddenly a list of properties appear.

# My Wpf App
Write-Host "WpfApp Versioning started"

$sourceDir = "C:\Users\me\source\repos\mysolution"

$csprojfilename = $sourceDir +"\myapp\myapp.csproj"
"Project file to update " + $csprojfilename

[xml]$csprojcontents = Get-Content -Path $csprojfilename;

"Current version number is " + $csprojcontents.Project.PropertyGroup.Version + "."
$oldversionNumber = $csprojcontents.Project.PropertyGroup.Version

# https://gist.github.com/bcnzer/f8d50699c5bf7614923bff733662cb1a
if ($csprojcontents.Project.PropertyGroup.name -notmatch "Version") 
{ 
    Write-Host "Version is null."
    $csprojcontents.Project.PropertyGroup.AppendChild($csprojcontents.ImportNode(([xml]"<Version/>").DocumentElement,$true)) 
}

Write-Host "Update Version."
$csprojcontents.SelectNodes("/Project/PropertyGroup/Version")[0].InnerText = [version]($ecatBuildNumber)

Write-Host "Save csproj."
$csprojcontents.Save($csprojfilename)

"Version number has been updated from " + $oldversionNumber + " to " + $ecatBuildNumber + "."

Write-Host "WpfApp Versioning Finished"

This is the output I get when using Powershell ISE.

WpfApp Versioning started

Project file to update C:\Users\me\source\repos\mysolution\myapp\myapp.csproj Current version number is 6.0.0.6 . Version is null.

Name : Version LocalName : Version NamespaceURI : Prefix : NodeType : Element ParentNode : PropertyGroup OwnerDocument : #document IsEmpty : True Attributes : {} HasAttributes : False SchemaInfo : System.Xml.XmlName InnerXml : InnerText : NextSibling
: PreviousSibling : Version Value : ChildNodes : {} FirstChild : LastChild : HasChildNodes : False IsReadOnly : False OuterXml : BaseURI
: PreviousText :

Name : Version LocalName : Version NamespaceURI : Prefix : NodeType : Element ParentNode : PropertyGroup OwnerDocument : #document IsEmpty : True Attributes : {} HasAttributes : False SchemaInfo : System.Xml.XmlName InnerXml : InnerText : NextSibling
: PreviousSibling : Version Value : ChildNodes : {} FirstChild : LastChild : HasChildNodes : False IsReadOnly : False OuterXml : BaseURI
: PreviousText :

Name : Version LocalName : Version NamespaceURI : Prefix : NodeType : Element ParentNode : PropertyGroup OwnerDocument : #document IsEmpty : True Attributes : {} HasAttributes : False SchemaInfo : System.Xml.XmlName InnerXml : InnerText : NextSibling
: PreviousSibling : Version Value : ChildNodes : {} FirstChild : LastChild : HasChildNodes : False IsReadOnly : False OuterXml : BaseURI
: PreviousText :

Name : Version LocalName : Version NamespaceURI : Prefix : NodeType : Element ParentNode : PropertyGroup OwnerDocument : #document IsEmpty : True Attributes : {} HasAttributes : False SchemaInfo : System.Xml.XmlName InnerXml : InnerText : NextSibling
: PreviousSibling : Version Value : ChildNodes : {} FirstChild : LastChild : HasChildNodes : False IsReadOnly : False OuterXml : BaseURI
: PreviousText :

Name : Version LocalName : Version NamespaceURI : Prefix : NodeType : Element ParentNode : PropertyGroup OwnerDocument : #document IsEmpty : True Attributes : {} HasAttributes : False SchemaInfo : System.Xml.XmlName InnerXml : InnerText : NextSibling
: PreviousSibling : Version Value : ChildNodes : {} FirstChild : LastChild : HasChildNodes : False IsReadOnly : False OuterXml : BaseURI
: PreviousText :

Name : Version LocalName : Version NamespaceURI : Prefix : NodeType : Element ParentNode : PropertyGroup OwnerDocument : #document IsEmpty : True Attributes : {} HasAttributes : False SchemaInfo : System.Xml.XmlName InnerXml : InnerText : NextSibling
: PreviousSibling : Version Value : ChildNodes : {} FirstChild : LastChild : HasChildNodes : False IsReadOnly : False OuterXml : BaseURI
: PreviousText :

Update Version. Save csproj. Version number has been updated from 6.0.0.6 to 6.0.0.6.

WpfApp Versioning Finished

It's strange. How do I get it to stop doing that?

On The Net Again
  • 265
  • 4
  • 16
  • 1
    In short: any output - be it from a PowerShell command or a .NET method call - that is neither captured in a variable nor redirected (sent through the pipeline or to a file) is _implicitly output_ from a script or function. To simply _discard_ such output, use `$null = ...`. If you don't discard such output, it becomes part of a script or function's "return value" (stream of output objects). See [this answer](https://stackoverflow.com/a/55665963/45375) to the linked duplicate. – mklement0 Oct 09 '21 at 18:30
  • 1
    Specifically, it is the [`.AppendChild()`](https://learn.microsoft.com/en-US/dotnet/api/system.xml.xmlnode.appendchild#System_Xml_XmlNode_AppendChild_System_Xml_XmlNode_) method call that produces implicit output in your case. – mklement0 Oct 09 '21 at 18:31
  • @mklement0 That linked answer did help explain what was going on and the why the solutions worked. Thanks again. – On The Net Again Oct 11 '21 at 11:14
  • Glad to hear it, @OnTheNetAgain; my pleasure. – mklement0 Oct 11 '21 at 14:17

1 Answers1

1

Thanks to your great logging, it is easy to isolate the error. Check your syntax on this line:

# https://gist.github.com/bcnzer/f8d50699c5bf7614923bff733662cb1a
if ($csprojcontents.Project.PropertyGroup.name -notmatch "Version") 
{ 
    Write-Host "Version is null."
    #this line below
    $csprojcontents.Project.PropertyGroup.AppendChild($csprojcontents.ImportNode(([xml]"<Version/>").DocumentElement,$true)) 
}

The syntax for importing a new node is pretty different than I've usually seen. I changed the syntax like this and it worked.

if ($csprojcontents.Project.PropertyGroup.name -notmatch "Version") 
{ 
    Write-Host "Version is null."
    $child = $csprojcontents.CreateElement("Version")
    $csprojcontents.Project.PropertyGroup.AppendChild($child) 
}

As for why you need to do this, this code first adds a Version node to make sure its there, and then tries to set the version to the right value.

FoxDeploy
  • 12,569
  • 2
  • 33
  • 48