为什么我在 powershell 中添加节点时一直获取属性列表?
Why do I keep getting a list of properties when adding a node in powershell?
我正在使用 powershell 更新 wpf csproj 文件中的版本节点。每次我添加版本节点(当它已经存在时我从来不知道我必须这样做),突然出现一个属性列表。
# 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"
这是我在使用 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
很奇怪。我如何让它停止这样做?
感谢您出色的日志记录,很容易找出错误。检查此行的语法:
# 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))
}
导入新节点的语法与我通常看到的完全不同。我像这样更改了语法并且它起作用了。
if ($csprojcontents.Project.PropertyGroup.name -notmatch "Version")
{
Write-Host "Version is null."
$child = $csprojcontents.CreateElement("Version")
$csprojcontents.Project.PropertyGroup.AppendChild($child)
}
至于为什么需要这样做,这段代码首先添加了一个 Version 节点以确保其存在,然后尝试将版本设置为正确的值。
我正在使用 powershell 更新 wpf csproj 文件中的版本节点。每次我添加版本节点(当它已经存在时我从来不知道我必须这样做),突然出现一个属性列表。
# 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"
这是我在使用 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
很奇怪。我如何让它停止这样做?
感谢您出色的日志记录,很容易找出错误。检查此行的语法:
# 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))
}
导入新节点的语法与我通常看到的完全不同。我像这样更改了语法并且它起作用了。
if ($csprojcontents.Project.PropertyGroup.name -notmatch "Version")
{
Write-Host "Version is null."
$child = $csprojcontents.CreateElement("Version")
$csprojcontents.Project.PropertyGroup.AppendChild($child)
}
至于为什么需要这样做,这段代码首先添加了一个 Version 节点以确保其存在,然后尝试将版本设置为正确的值。