XmlFile Class

MSBuild Extension Pack

XmlFile Class MSBuild Extension Pack Help 4.0.12.0
Valid TaskActions are:

AddAttribute (Required: File, Element or XPath, Key, Value Optional:Prefix, Namespaces, RetryCount)

AddElement (Required: File, Element and ParentElement or Element and XPath, Optional: Prefix, Key, Value, Namespaces, RetryCount, InnerText, InnerXml, InsertBeforeXPath / InsertAfterXPath)

ReadAttribute (Required: File, XPath Optional: Namespaces Output: Value)

ReadElements (Required: File, XPath Optional: Namespaces, ReadChildrenToMetadata Output: Elements). Attributes are added as metadata. Use ReadChildrenToMetadata to add first level children as metadata

ReadElementText (Required: File, XPath Optional: Namespaces Output: Value)

ReadElementXml (Required: File, XPath Optional: Namespaces Output: Value)

RemoveAttribute (Required: File, Key, Element or XPath Optional: Namespaces, RetryCount)

RemoveElement (Required: File, Element and ParentElement or Element and XPath Optional: Namespaces, RetryCount)

UpdateAttribute (Required: File, XPath Optional: Namespaces, Key, Value, RetryCount)

UpdateElement (Required: File, XPath Optional: Namespaces, InnerText, InnerXml, RetryCount)

Remote Execution Support: NA

Inheritance Hierarchy

SystemObject  Microsoft.Build.UtilitiesTask
    MSBuild.ExtensionPackBaseTask
      MSBuild.ExtensionPack.XmlXmlFile

Namespace: MSBuild.ExtensionPack.Xml
Assembly: MSBuild.ExtensionPack (in MSBuild.ExtensionPack.dll) Version: 4.0.0.0

The XmlFile type exposes the following members.

Constructors

  NameDescription
Public methodXmlFile
Top
Properties

  NameDescription
Public propertyElement
Sets the element. For AddElement, if the element exists, it's InnerText / InnerXml will be updated
Public propertyElements
Gets the elements selected using ReadElements
Public propertyFile
Sets the file.
Public propertyInnerText
Sets the InnerText.
Public propertyInnerXml
Sets the InnerXml.
Public propertyInsertAfterXPath
Specifies the XPath to be used to control where a new element is added. The Xpath must resolve to single node.
Public propertyInsertBeforeXPath
Specifies the XPath to be used to control where a new element is added. The Xpath must resolve to single node.
Public propertyKey
Sets the Attribute key.
Public propertyNamespaces
TaskItems specifiying "Prefix" and "Uri" attributes for use with the specified XPath
Public propertyParentElement
Sets the parent element.
Public propertyPrefix
Sets the Prefix used for an added element/attribute, prefix must exists in Namespaces.
Public propertyReadChildrenToMetadata
When using ReadElements, specifies whether the first level child elements should be added as metadata. Child elements will override any read attributes of the same name. Default is false.
Public propertyRetryCount
Sets a value indicating how many times to retry saving the file, e.g. if files are temporarily locked. Default is 5. The retry occurs every 5 seconds.
Public propertyValue
Gets or Sets the Attribute key value. Also stores the result of any Read TaskActions
Public propertyXPath
Specifies the XPath to be used
Top
Examples

<Project ToolsVersion="4.0" DefaultTargets="Default" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <PropertyGroup>
        <TPath>$(MSBuildProjectDirectory)\..\MSBuild.ExtensionPack.tasks</TPath>
        <TPath Condition="Exists('$(MSBuildProjectDirectory)\..\..\Common\MSBuild.ExtensionPack.tasks')">$(MSBuildProjectDirectory)\..\..\Common\MSBuild.ExtensionPack.tasks</TPath>
    </PropertyGroup>
    <Import Project="$(TPath)"/>
    <ItemGroup>
        <ConfigSettingsToDeploy Include="c:\machine.config">
            <Action>RemoveElement</Action>
            <Element>processModel</Element>
            <ParentElement>/configuration/system.web</ParentElement>
        </ConfigSettingsToDeploy>
        <ConfigSettingsToDeploy Include="c:\machine.config">
            <Action>AddElement</Action>
            <Element>processModel</Element>
            <ParentElement>/configuration/system.web</ParentElement>
        </ConfigSettingsToDeploy>
        <ConfigSettingsToDeploy Include="c:\machine.config">
            <Action>AddAttribute</Action>
            <Key>enable</Key>
            <ValueToAdd>true</ValueToAdd>
            <Element>/configuration/system.web/processModel</Element>
        </ConfigSettingsToDeploy>
        <ConfigSettingsToDeploy Include="c:\machine.config">
            <Action>AddAttribute</Action>
            <Key>timeout</Key>
            <ValueToAdd>Infinite</ValueToAdd>
            <Element>/configuration/system.web/processModel</Element>
        </ConfigSettingsToDeploy>
        <ConfigSettingsToDeploy Include="c:\machine.config">
            <Action>RemoveAttribute</Action>
            <Key>timeout</Key>
            <Element>/configuration/system.web/processModel</Element>
        </ConfigSettingsToDeploy>
        <XMLConfigElementsToAdd Include="c:\machine.config">
            <XPath>/configuration/configSections</XPath>
            <Name>section</Name>
            <KeyAttributeName>name</KeyAttributeName>
            <KeyAttributeValue>enterpriseLibrary.ConfigurationSource</KeyAttributeValue>
        </XMLConfigElementsToAdd>
        <XMLConfigElementsToAdd Include="c:\machine.config">
            <XPath>/configuration</XPath>
            <Name>enterpriseLibrary.ConfigurationSource</Name>
            <KeyAttributeName>selectedSource</KeyAttributeName>
            <KeyAttributeValue>MyKeyAttribute</KeyAttributeValue>
        </XMLConfigElementsToAdd>
        <XMLConfigElementsToAdd Include="c:\machine.config">
            <XPath>/configuration/enterpriseLibrary.ConfigurationSource</XPath>
            <Name>sources</Name>
        </XMLConfigElementsToAdd>
        <XMLConfigElementsToAdd Include="c:\machine.config">
            <XPath>/configuration/enterpriseLibrary.ConfigurationSource/sources</XPath>
            <Name>add</Name>
            <KeyAttributeName>name</KeyAttributeName>
            <KeyAttributeValue>MyKeyAttribute</KeyAttributeValue>
        </XMLConfigElementsToAdd>
        <XMLConfigAttributesToAdd Include="c:\machine.config">
            <XPath>/configuration/configSections/section[@name='enterpriseLibrary.ConfigurationSource']</XPath>
            <Name>type</Name>
            <Value>Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ConfigurationSourceSection, Microsoft.Practices.EnterpriseLibrary.Common, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35</Value>
        </XMLConfigAttributesToAdd>
        <XMLConfigAttributesToAdd Include="c:\machine.config">
            <XPath>/configuration/enterpriseLibrary.ConfigurationSource/sources/add[@name='MyKeyAttribute']</XPath>
            <Name>type</Name>
            <Value>MyKeyAttribute.Common, MyKeyAttribute.Common, Version=1.0.0.0, Culture=neutral, PublicKeyToken=fb2f49125f05d89</Value>
        </XMLConfigAttributesToAdd>
        <XMLConfigElementsToDelete Include="c:\machine.config">
            <XPath>/configuration/configSections/section[@name='enterpriseLibrary.ConfigurationSource']</XPath>
        </XMLConfigElementsToDelete>
        <XMLConfigElementsToDelete Include="c:\machine.config">
            <XPath>/configuration/enterpriseLibrary.ConfigurationSource[@selectedSource='MyKeyAttribute']</XPath>
        </XMLConfigElementsToDelete>
    </ItemGroup>
    <Target Name="Default">
        <!-- Work through some manipulations that don't use XPath-->
        <MSBuild.ExtensionPack.Xml.XmlFile TaskAction="%(ConfigSettingsToDeploy.Action)" File="%(ConfigSettingsToDeploy.Identity)" Key="%(ConfigSettingsToDeploy.Key)" Value="%(ConfigSettingsToDeploy.ValueToAdd)" Element="%(ConfigSettingsToDeploy.Element)" ParentElement="%(ConfigSettingsToDeploy.ParentElement)" Condition="'%(ConfigSettingsToDeploy.Identity)'!=''"/>
        <!-- Work through some manipulations that use XPath-->
        <MSBuild.ExtensionPack.Xml.XmlFile TaskAction="RemoveElement" File="%(XMLConfigElementsToDelete.Identity)" XPath="%(XMLConfigElementsToDelete.XPath)" Condition="'%(XMLConfigElementsToDelete.Identity)'!=''"/>
        <MSBuild.ExtensionPack.Xml.XmlFile TaskAction="AddElement" File="%(XMLConfigElementsToAdd.Identity)" Key="%(XMLConfigElementsToAdd.KeyAttributeName)" Value="%(XMLConfigElementsToAdd.KeyAttributeValue)" Element="%(XMLConfigElementsToAdd.Name)" XPath="%(XMLConfigElementsToAdd.XPath)" Condition="'%(XMLConfigElementsToAdd.Identity)'!=''"/>
        <MSBuild.ExtensionPack.Xml.XmlFile TaskAction="AddAttribute" File="%(XMLConfigAttributesToAdd.Identity)" Key="%(XMLConfigAttributesToAdd.Name)" Value="%(XMLConfigAttributesToAdd.Value)" XPath="%(XMLConfigAttributesToAdd.XPath)" Condition="'%(XMLConfigAttributesToAdd.Identity)'!=''"/>
        <MSBuild.ExtensionPack.Xml.XmlFile TaskAction="UpdateElement" File="c:\machine.config" XPath="/configuration/configSections/section[@name='system.data']" InnerText="NewValue"/>
        <MSBuild.ExtensionPack.Xml.XmlFile TaskAction="UpdateAttribute" File="c:\machine.config" XPath="/configuration/configSections/section[@name='system.data']" Key="SomeAttribute" Value="NewValue"/>
    </Target>
    <!-- The following illustrates Namespace usage -->
    <ItemGroup>
        <Namespaces Include="Mynamespace">
            <Prefix>me</Prefix>
            <Uri>http://mynamespace</Uri>
        </Namespaces>
        <XMLConfigElementsToDelete1 Include="c:\test.xml">
            <XPath>//me:MyNodes/me:sources</XPath>
        </XMLConfigElementsToDelete1>
        <XMLConfigElementsToAdd1 Include="c:\test.xml">
            <XPath>//me:MyNodes</XPath>
            <Name>sources</Name>
        </XMLConfigElementsToAdd1>
    </ItemGroup>
    <Target Name="DefaultWithNameSpace">
        <MSBuild.ExtensionPack.Xml.XmlFile TaskAction="RemoveElement" Namespaces="@(Namespaces)" File="%(XMLConfigElementsToDelete1.Identity)" XPath="%(XMLConfigElementsToDelete1.XPath)" Condition="'%(XMLConfigElementsToDelete1.Identity)'!=''"/>
        <MSBuild.ExtensionPack.Xml.XmlFile TaskAction="AddElement" Namespaces="@(Namespaces)" File="%(XMLConfigElementsToAdd1.Identity)" Key="%(XMLConfigElementsToAdd1.KeyAttributeName)" Value="%(XMLConfigElementsToAdd1.KeyAttributeValue)" Element="%(XMLConfigElementsToAdd1.Name)" XPath="%(XMLConfigElementsToAdd1.XPath)" Condition="'%(XMLConfigElementsToAdd1.Identity)'!=''"/>
    </Target>
    <ItemGroup>
        <Namespaces2 Include="Mynamespace">
            <Prefix>xs</Prefix>
            <Uri>http://www.w3.org/2001/XMLSchema</Uri>
        </Namespaces2>
    </ItemGroup>
    <Target Name="InsertBeforeXPath">
        <MSBuild.ExtensionPack.Xml.XmlFile TaskAction="AddElement"
        File="d:\a\tempinsertbeforexpath.xml"
        Namespaces="@(Namespaces2)"
        ParentElement="/xs:schema"
        Prefix="xs"
        Element="test"
        Key="name"
        Value="new"
        InsertBeforeXPath="/xs:schema/xs:log[@name='logger']"/>
    </Target>
</Project>
See Also

Reference