1

我需要一些关于 SQL 查询的帮助,该查询将返回包含特定自定义参数的构建定义列表。

我遇到了这个查询,但我很难弄清楚如何进一步向下钻取。

;WITH XMLNAMESPACES('clr-namespace:Microsoft.TeamFoundation.Build.Workflow.Activities;assembly=Microsoft.TeamFoundation.Build.Workflow' AS mtbwa, 
DEFAULT 'clr-namespace:System.Collections.Generic;assembly=mscorlib'), 
q AS (
  SELECT CAST(bd.ProcessParameters AS XML) p
  FROM dbo.tbl_BuildDefinition bd     
  ) 
SELECT  
  X.Doc.query('mtbwa:BuildSettings') AS 'Node',
  X.Doc.value('(mtbwa:BuildSettings/@ProjectsToBuild)[1]', 'VARCHAR(100)') AS 'ProjectsToBuild'
FROM q 
CROSS APPLY p.nodes('/Dictionary') AS X(Doc)

以下是此查询返回的 XML 示例:

<Dictionary x:TypeArguments="x:String, x:Object" xmlns="clr-namespace:System.Collections.Generic;assembly=mscorlib" xmlns:mtbwa="clr-namespace:Microsoft.TeamFoundation.Build.Workflow.Activities;assembly=Microsoft.TeamFoundation.Build.Workflow" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <mtbwa:BuildSettings x:Key="BuildSettings" ProjectsToBuild="$/ACME/Dev/v1.0.0/Source/ACME/ACME.sln">
    <mtbwa:BuildSettings.PlatformConfigurations>
      <mtbwa:PlatformConfigurationList Capacity="1">
        <mtbwa:PlatformConfiguration Configuration="ACMS Website" Platform="Any CPU" />
      </mtbwa:PlatformConfigurationList>
     </mtbwa:BuildSettings.PlatformConfigurations>
   </mtbwa:BuildSettings>
   <mtbwa:TestSpecList x:Key="TestSpecs" Capacity="1">
     <mtbwa:TestAssemblySpec MSTestCommandLineArgs="{x:Null}" TestSettingsFileName="{x:Null}" AssemblyFileSpec="**\*test*.dll" CategoryFilter="!CodedUITest" />
   </mtbwa:TestSpecList>
   <mtbwa:SourceAndSymbolServerSettings x:Key="SourceAndSymbolServerSettings" SymbolStorePath="\\ACME.com\dfs\USA\AppDev\TeamBuild\Symbols" />
   <mtbwa:AgentSettings x:Key="AgentSettings" MaxWaitTime="04:00:00" Name="*" Tags="" />
   <x:Boolean x:Key="SCA">False</x:Boolean>
   <x:String x:Key="SSCProjectVersion">ACME v1.0.0</x:String>
</Dictionary>

我正在尝试获取<x:Boolean x:Key="SCA">False</x:Boolean>返回的 XML 的一部分。任何关于我如何实现这一点的想法都会受到赞赏。

解决方案
感谢 jessehouwing 提出的建议。

TfsTeamProjectCollection server = new TfsTeamProjectCollection(new Uri("http://acmetfs:8080/tfs"));
server.EnsureAuthenticated();
IBuildServer build = (IBuildServer)server.GetService(typeof(IBuildServer));
IBuildDefinition buildDefinition = build.GetBuildDefinition("ACME", "ACME_v1.0.0");

object argumentValue;
if (WorkflowHelpers.DeserializeProcessParameters(buildDefinition.ProcessParameters).TryGetValue("SCA", out argumentValue))
{
  Console.WriteLine(argumentValue);
}        
4

1 回答 1

2

警告

不建议直接查询 TFS 操作数据存储,这些字段的内容可能会在版本甚至修补程序之间更改,恕不另行通知。执行此操作的官方方法是使用客户端对象模型。您不小心对数据进行的任何更改都会使您的服务器处于不受支持的状态。

TFS 客户端对象模型提供了一个相对简单的 API 来查询构建定义。从那里您可以使用标准的 .NET XML 类(Linq-2-XML 或标准 XPathNavigator)来快速获取您需要的信息。您要查找的参数甚至可能存储在构建元数据中。

这将导致以下代码(由 OP 提供):

TfsTeamProjectCollection server = new TfsTeamProjectCollection(new Uri("http://acmetfs:8080/tfs"));
server.EnsureAuthenticated();
IBuildServer build = (IBuildServer)server.GetService(typeof(IBuildServer));
IBuildDefinition buildDefinition = build.GetBuildDefinition("ACME", "ACME_v1.0.0");

object argumentValue;
if (WorkflowHelpers.DeserializeProcessParameters(buildDefinition.ProcessParameters).TryGetValue("SCA", out argumentValue))
{
  Console.WriteLine(argumentValue);
}    
于 2014-03-05T19:29:09.050 回答