11

I'm parsing a csproj file for a nuget install and I've got a node that needs to be altered. The node in question is the node named "Generator" where it's value equals "TextTemplatingFileGenerator" and it's parent node has an attribute of "WebConfigSettingsGeneratorScript.tt" (the second part isn't in here yet).

Here's the script that I've got, but it's not quite done. It's working, but it saves an empty file. Also, it doesn't have the 2nd part of my where clause, which is

$path = 'C:\Projects\Intouch\NuGetTestPackage\NuGetTestPackage'
cd $path
$files = get-childitem -recurse -filter *.csproj
foreach ($file in $files){
    ""
    "Filename: {0}" -f $($file.Name)
    "=" * ($($file.FullName.Length) + 10)   

    if($file.Name -eq 'NuGetTestPackage1.csproj'){
        $xml = gc $file.FullName | 
        Where-Object { $_.Project.ItemGroup.None.Generator -eq 'TextTemplatingFileGenerator' } | 
        ForEach-Object { $_.Project.ItemGroup.None.Generator = '' }
        Set-Content $file.FullName $xml
    }   
}

Here's a basic version of the XML:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <ItemGroup>
    <None Include="T4\WebConfigSettingGeneratorScript.tt">
      <Generator>TextTemplatingFileGenerator</Generator>
      <LastGenOutput>WebConfigSettingGeneratorScript.txt</LastGenOutput>
    </None>

Thanks a lot. I'm a total PowerShell n00b!

3 Answers 3

30

As @empo says, you need to cast the output of gc $file.FullName to [xml] e.g. $xml = [xml](gc $file.FullName). And then after making the change but before looping to the next file, you need to save the file e.g. $xml.Save($file.FullName).

This works with the sample project you provided:

$file = gi .\test.csproj
$pattern = 'TextTemplatingFileGenerator'
$xml = [xml](gc $file)
$xml | Where {$_.Project.ItemGroup.None.Generator -eq $pattern} |
       Foreach {$_.Project.ItemGroup.None.Generator = ''}
$xml.Save($file.Fullname)
Sign up to request clarification or add additional context in comments.

6 Comments

This helps, but $xml.Save($file.FullName) is coming up as an Invalid Operation.
The only exception XmlDocument.Save(string path) is documented to throw is a XmlException if the corresponding XmlDocument is not well formed. BTW you should also get rid of the Set-Content $file.FullName $xml since the $xml.Save($file.Fullname) call will save back over the original file.
Great, I've taken the changes that you have offered. But, it doesn't seem to be making the change to the file. However, the script IS saving the file.
The issue with this script is really just that my XML is more complicated than the bit that I provided and that's the real issue. Here's a simpler version of this question: stackoverflow.com/questions/6049323/…
@DonRolling did you find out why the .csproj file was overwritten? I have the same issue when trying to update my .csproj file using NuGet package script tools\install.ps1
|
5

Are you missing a cast?

$xml = [xml] gc $file.FullName

Comments

0

OK, now my change is working:

    $fileName = “C:\sovgarde\updates.xml”;
$xml = [System.Xml.XmlDocument](Get-Content $fileName);

$child = $xml.CreateElement('option')
$child.SetAttribute('name', 'CHECK_NEEDED')
$child.SetAttribute('value','false')
If ($xml.application.component.option.name -icontains "CHECK_NEEDED") {
    
    $xml.SelectNodes("//option[@name=`"CHECK_NEEDED`"]") | % {$_.ParentNode.removechild($_) }
    #$xml.Save($fileName)   
    
}
$node = $xml.SelectSingleNode('//component')
$node.AppendChild($child)
$xml.Save($fileName)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.