Thursday, 15 May 2014

Setting xml value in powershell failing with Property '...' cannot be found on this object -



Setting xml value in powershell failing with Property '...' cannot be found on this object -

i trying load csproj file , alter it's root namespace in powershell.

the xml of csproj file looks like:

<project...xmlsns=" > <propertygroup> <rootnamespace>somenamespace</rootnamespace>

...

i can value, navigating property

> $xmldoc = (get-content myproject.csproj) > $xmldoc.project.propertygroup.rootnamespace ## outputs somenamespace

but can't assign - setting xml value in powershell failing property '...' cannot found on object...

i have tried handcrafted xml file , succeeded

<test> <testinner1> <testinner2>somevalue</testinner2> </testinner1> </test> >$xmldoc = [xml](get-content test.xml) >$xmldoc.test.testinner1.testinner2 = "some other value" >$xmldoc.test.testinner1.testinner2 ## returns other value

i have modified elements under testinner2 - added additional elements, self closed elements. i've added namespace test. still able set value in each of these cases.

when in powershell ise, getting intellisense types note in test xml each of test, testinner1 , testinner2 xmlelements. on project file project , propertygroup xmlelements - intellisense stops , doesn't provide rootnamespace. when type of propertygroup it's object[], , of rootnamespace string. rootnamespace claims have { get; set; } aforementioned error on setting.

if there workaround works on project file i'm interested, i'm as interested know why 2 examples above differ i.e missing !

there more 1 propertygroup node under project node. usually.

xmldocument class homecoming first element if there one, if there more require index. test illustration works because there 1 sub element name testinner2. if know position of propertygroup element can access index

$xmldoc.project.propertygroup[0].rootnamespace

or utilize forcefulness bend xml will

($xmldoc.project.propertygroup | where-object { $_['rootnamespace'] -ne $null}).rootnamespace = "new value"

xml powershell

No comments:

Post a Comment