0

I have a powershell script I'll be using to enhance a publish to folder profile. I want to be able to read my appsettings.json 'versionNumber' property and move the resulting file around and create a nice zip. I realize I can do all this with MSBUILD directly, but this is for a .net core project and im concerned about not doing it correctly. So for now I'm using a PS script AND a publish profile.

The powershell script refuses to read in the publish profile xml. Here is the PS script:

$foundPublishProfile = Test-Path '..\Properties\PublishProfiles\FolderProfile.pubxml'
if (!$foundPublishProfile)
{
    Write-Host 'ERROR: Expected to find "..\Properties\PublishProfiles\FolderProfile.pubxml"' -ForegroundColor DarkRed
    Write-Host ''
    exit
}

[xml] $publishProfileXML = Get-Content '..\Properties\PublishProfiles\FolderProfile.pubxml'
Write-Host $publishProfileXML
$publishFilePath = $publishProfileXML.SelectSingleNode('//Project/PropertyGroup/PublishUrl') #| Select-Object -Expand '#text'
#$publishFilePath = Select-Xml -XPath '//Project/PropertyGroup/PublishUrl' -Path '..\Properties\PublishProfiles\FolderProfile.pubxml'
Write-Host $publishFilePath

And here is the publish profile, pubxml file:

<?xml version="1.0" encoding="utf-8"?>
<!--
https://go.microsoft.com/fwlink/?LinkID=208121. 
-->
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <DeleteExistingFiles>False</DeleteExistingFiles>
    <ExcludeApp_Data>False</ExcludeApp_Data>
    <LaunchSiteAfterPublish>True</LaunchSiteAfterPublish>
    <LastUsedBuildConfiguration>Release</LastUsedBuildConfiguration>
    <LastUsedPlatform>Any CPU</LastUsedPlatform>
    <PublishProvider>FileSystem</PublishProvider>
    <PublishUrl>F:\Fileshare\NCHIWebSite</PublishUrl>
    <WebPublishMethod>FileSystem</WebPublishMethod>
    <SiteUrlToLaunchAfterPublish />
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <ProjectGuid>f7da8cd0-d62c-47c5-9ae7-7c9cb9a6d23d</ProjectGuid>
    <SelfContained>false</SelfContained>
  </PropertyGroup>
</Project>

I expected to see something for the output, whether its from the loaded XML content or from my XPATH select single node, but instead I'm getting nothing. I've tried many variations. Is there something I'm doing incorrectly?

Iofacture
  • 655
  • 3
  • 13
  • 39

1 Answers1

2

Your input XML uses namespaces, which you must account for when you use XPath queries with .SelectSingleNode() - elements that are part of a namespace cannot be located by their mere element name (such as Project), even if they're only implicitly part of a namespace via an ancestral xmlns attribute.

However, it is often simpler to use PowerShell's convenient, property-based adaptation of the XML DOM, which allows you to use simple dot notation to drill down to the element(s) of interest, and which ignores namespaces:

$publishProfileXM.Project.PropertyGroup.PublishUrl

See this answer for additional information, including how to alternatively use the Select-Xml cmdlet with XPath queries and namespaces.

mklement0
  • 382,024
  • 64
  • 607
  • 775
  • 1
    Thank you so much - I always think that namespaces somehow don't apply as long as there's no element prefix, but apparently they're an issue just existing. The ignore namespace methodology is a life saver when the xml is small and known. I appreciate it! – Iofacture Nov 19 '20 at 23:55
  • @Iofacture: Glad to hear the answer was helpful; my pleasure. – mklement0 Nov 19 '20 at 23:59