Wednesday, 15 August 2012

c# - MSBuild - XPath - XmlPeek can read but XmlPoke can not write -



c# - MSBuild - XPath - XmlPeek can read but XmlPoke can not write -

i'm using msbuild manipulate project (.csproj) file update reference static file. static file built ci server (teamcity) , reference project uses need updated before project built.

here illustration of xml csproj file (full version):

<?xml version="1.0" encoding="utf-8"?> <project defaulttargets="build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" toolsversion="12.0"> <itemgroup> <content include="packages\pmixins.0.1.7.nupkg"> <includeinvsix>true</includeinvsix> </content>

i have written msbuild task:

<target name="replacenugetpackagedependency" beforetargets="prepareforbuild" > <xmlpoke xmlinputpath="$(msbuildprojectfile)" query="//n:project/n:itemgroup/ n:content[starts-with(@include, 'packages')]/@include" value="test-test" namespaces="&lt;namespace prefix='n' uri='http://schemas.microsoft.com/developer/msbuild/2003' name='donotknowwhatthisisfor-butitisrequired' /&gt;" > </xmlpoke> </target>

but when run message 0 replacements.

so added xmlpeek task test query:

<xmlpeek xmlinputpath="$(msbuildprojectfile)" query="/n:project/n:itemgroup/ n:content[starts-with(@include, 'packages')]/@include" namespaces="&lt;namespace prefix='n' uri='http://schemas.microsoft.com/developer/msbuild/2003' name='donotknowwhatthisisfor-butitisrequired' /&gt;"> <output taskparameter="result" itemname="peeked" /> </xmlpeek> <message text="text: @(peeked)"/>

when run msbuild xmlpeek able read xml:

text: packages\pmixins.0.1.7.nupkg

the queries exactly same! why can't xmlpoke manipulate xml if xmlpeek can read it?

update

after hours of playing this, found xpath query xmlpoke want:

query="//n:project/n:itemgroup/ n:content[starts-with(@include, 'packages')]/n:includeinvsix/../@include"

why necessary add together /n:includeinvsix/..? bug??

just wanted confirm else encounters this, how, in fact, around issue of not beingness able utilize same exact xpath query in xmlpeek task , xmlpoke task.

original query replace "file" attribute value of appsettings element in regular web.config:

<appsettings file="devsettings.config"> <add key="buildversion" value="" /> </appsettings>

to @ "file" attribute in xmlpeek task used next xpath query:

//appsettings[@file='devsettings.config']/@file

however same query wouldn't work in xmlpoke task. instead next worked @philip-pittle discovered in update question

//appsettings[@file='devsettings.config']/add/../@file

<xmlpeek xmlinputpath="$(_buildpath)web.config" query="//appsettings[@file='devsettings.config']/@file"> <output taskparameter="result" itemname="existingpeeked" /> </xmlpeek> <xmlpoke xmlinputpath="$(_buildpath)web.config" query="//appsettings[@file='devsettings.config']/add/../@file" value="$(_environmentconfig)" />

this using next versions of msbuild.

microsoft (r) build engine version 12.0.31101.0

[microsoft .net framework, version 4.0.30319.18444]

may it's bug? odd behavior.

c# .net xml xpath msbuild

No comments:

Post a Comment