Skip to content

Instantly share code, notes, and snippets.

@lavn0
Created July 10, 2016 08:51
Show Gist options
  • Save lavn0/64da07946e2097ee1fe5b5bd845d15a1 to your computer and use it in GitHub Desktop.
Save lavn0/64da07946e2097ee1fe5b5bd845d15a1 to your computer and use it in GitHub Desktop.
Xmlの値を読み取るMSBuildタスクを定義するコード
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<UsingTask TaskName="XmlRead" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
<ParameterGroup>
<XmlFileName ParameterType="System.String" Required="true" />
<XPath ParameterType="System.String" Required="true" />
<Result ParameterType="System.String" Output="true" />
</ParameterGroup>
<Task>
<Reference Include="System" />
<Reference Include="System.Xml" />
<Reference Include="System.Xml.Linq" />
<Using Namespace="System.Collections.Generic" />
<Using Namespace="System.Xml.Linq" />
<Using Namespace="System.Xml.XPath" />
<Code Type="Fragment" Language="cs"><![CDATA[
var xdoc = XDocument.Load(XmlFileName);
var nodes = (IEnumerable<object>)xdoc.XPathEvaluate(XPath);
var result = nodes.First() as XText;
Result = result.Value;
]]></Code>
</Task>
</UsingTask>
<!-- Sample Code
<Target Name="AfterBuild" Condition="'$(Configuration)' == 'Release'">
<XmlRead XmlFileName="sample.xml" XPath="/root/sub/content/text()">
<Output TaskParameter="Result" PropertyName="contentValue" />
</XmlRead>
<Message Text="contentValue ::$(contentValue)" Importance="high" />
</Target>
-->
</Project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment