MSBuild.ExtensionPack.Xml.XmlFile - AddElement doesn't support duplicate element names?

Nov 3, 2010 at 10:30 AM

I am attempting to add new elements (IPermission) to an exisiting xml file as follows:

<root>
  <PermissionSet class="System.Security.PermissionSet" version="1" ID="Custom" SameSite="site">
    <IPermission class="System.Security.Permissions.EnvironmentPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Read="USERNAME" />
    <IPermission class="System.Security.Permissions.FileDialogPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
    <IPermission class="System.Security.Permissions.IsolatedStorageFilePermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Allowed="AssemblyIsolationByUser" UserQuota="9223372036854775807" Expiry="9223372036854775807" Permanent="True" />
    <IPermission class="System.Security.Permissions.ReflectionPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="ReflectionEmit" />
    <IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="Assertion, Execution, BindingRedirects" />
    <IPermission class="System.Security.Permissions.UIPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
    <IPermission class="System.Net.DnsPermission, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
    <IPermission class="System.Drawing.Printing.PrintingPermission, System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" version="1" Level="DefaultPrinting" />
  </PermissionSet>
</root>

The method I am using is the following: 

 <MSBuild.ExtensionPack.Xml.XmlFile
   TaskAction="AddElement"
   Namespaces="@(Namespaces)"
   File="afile.xml"
   Key="class"
   Value="System.Security.Permissions.MediaPermission, WindowsBase, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
   Element="IPermission"
   ParentElement="root/PermissionSet"/>

 

Also, I wanted to add an attribute to this new element by using the AddAttribute taskaction but doesn't appear to work?

 <MSBuild.ExtensionPack.Xml.XmlFile
   TaskAction="AddAttribute"
   Namespaces="@(Namespaces)"
   File="afile.xml"
   Key="version"
   Value="1"
   XPath="//def:IPermission[@class='System.Security.Permissions.MediaPermission, WindowsBase, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35']"/>

Any suggestions on this would be much appreciated.

Cheers

Eric

Coordinator
Nov 4, 2010 at 10:07 AM
Edited Nov 4, 2010 at 10:07 AM

It kind of does. Some history here: http://msbuildextensionpack.codeplex.com/Thread/View.aspx?ThreadId=222002

In your example, switch to XPath for #1

 

        <MSBuild.ExtensionPack.Xml.XmlFile 
            TaskAction="AddElement" 
            File="C:\a\permissionset.xml" 
            Key="class" 
            Value="System.Security.Permissions.MediaPermission, WindowsBase, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
            Element="IPermission"
            XPath="root/PermissionSet"
            />

 

Update your XPath for #2

 

        <MSBuild.ExtensionPack.Xml.XmlFile
   TaskAction="AddAttribute"
   Namespaces="@(Namespaces)"
            File="C:\a\permissionset.xml" 
   Key="version"
   Value="1"
   XPath="root/PermissionSet/IPermission[@class='System.Security.Permissions.MediaPermission, WindowsBase, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35']"/>

 

Sorry for the confusion. I'll add something to the docs.

Mike