how to read element value or attribute value

Jan 31, 2011 at 9:48 PM

Hello,

I'm trying to use the XMLFile target to read an xml element value or attribute value.

I've looked at the examples and they don't contain an example of this (to me) rather necessary syntax.

I've also inspected the dll with reflector and its not obvious how the value of the node is actually returned to the target.

ItemGroup Label="GLOBALS">
 <SourceProtocol Include="$(MSBuildProjectDirectory)\CPPTest1.config">
            <XPath>/configuration/globalPropertyGroup</XPath>
            <Name>sourceProtocol</Name>
            <Value>https://</Value>
 </SourceProtocol>
</ItemGroup>
<Target Name="Default">
  <MSBuild.ExtensionPack.Xml.XmlFile
        TaskAction="ReadAttribute"
        File="%(SourceProtocol.Identity)"
        XPath="%(SourceProtocol.XPath)"
        Element="%(SourceProtocol.Name)"
        Key="%(SourceProtocol.Name)"
        Value="%(SourceProtocol.Value)" />
</Target>

 Could you give me an example please?

Thanks,

Kimball

 

Coordinator
Feb 1, 2011 at 10:20 AM

Hi Kimball

Given

 

<?xml version="1.0"?>
<configuration>
  <globalPropertyGroup sourceProtocol="http://">Hello <br/> World</globalPropertyGroup>
</configuration>

 

and using

 

<Project ToolsVersion="3.5" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <Import Project="$(MSBuildExtensionsPath)\ExtensionPack\MSBuild.ExtensionPack.tasks"/>
    <Target Name="Default">
        <!--  ReadAttribute  -->
        <MSBuild.ExtensionPack.Xml.XmlFile TaskAction="ReadAttribute" File="c:\a\CPPTest1.config" XPath="/configuration/globalPropertyGroup/@sourceProtocol">
            <Output PropertyName="MyValue" TaskParameter="Value"/>
        </MSBuild.ExtensionPack.Xml.XmlFile>
        <Message Text="SourceProtocol = $(MyValue)"/>
        <!--  ReadElementText  -->
        <MSBuild.ExtensionPack.Xml.XmlFile TaskAction="ReadElementText" File="c:\a\CPPTest1.config" XPath="/configuration/globalPropertyGroup">
            <Output PropertyName="MyValue" TaskParameter="Value"/>
        </MSBuild.ExtensionPack.Xml.XmlFile>
        <Message Text="globalPropertyGroup Text = $(MyValue)"/>
        <!--  ReadElementXml  -->
        <MSBuild.ExtensionPack.Xml.XmlFile TaskAction="ReadElementXml" File="c:\a\CPPTest1.config" XPath="/configuration/globalPropertyGroup">
            <Output PropertyName="MyValue" TaskParameter="Value"/>
        </MSBuild.ExtensionPack.Xml.XmlFile>
        <Message Text="globalPropertyGroup Xml = $(MyValue)"/>
    </Target>
</Project>

 

You would get

 

Default:

XmlFile: c:\a\CPPTest1.config

Read Attribute: /configuration/globalPropertyGroup/@sourceProtocol

SourceProtocol = http://

XmlFile: c:\a\CPPTest1.config

Read Element: /configuration/globalPropertyGroup

globalPropertyGroupText = Hello  World  XmlFile: c:\a\CPPTest1.config

Read Element: /configuration/globalPropertyGroup

globalPropertyGroup Xml = Hello <br /> World

Mike

Feb 2, 2011 at 4:41 PM

Very kind of you.

Thanks,

Kimball

Sep 15, 2011 at 2:29 PM

Hi,

Is there a way to read attributes as ItemGroup for XML file like the folowwing:

<Files>

<File Path="somePath1"/>

<File Path="somePath2"/>

...

<Files>

Coordinator
Sep 15, 2011 at 3:30 PM

Can you elaborate....

Sep 15, 2011 at 3:40 PM

I have xml file looks like

<Files>

<File Path="somePath1"/>

<File Path="somePath2"/>

...

<Files>

In build I need to read attribute Path into ItemGroup for each of "File" element under "Files" element.

Coordinator
Oct 31, 2011 at 9:34 PM

I don't think that's supported by the current tasks. I'll log it and add it to the backlog.

Coordinator
Oct 31, 2011 at 9:37 PM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.
Coordinator
Nov 7, 2011 at 9:56 PM

KinslayeR, please let me know if http://msbuildextensionpack.codeplex.com/SourceControl/changeset/changes/71163 resolves this for you.

Mike