2017-01-23 125 views
0

設置的長度始終爲0,所以代碼跳轉到if語句的返回值不應該出現,我無法弄清楚爲什麼,屬性設置爲「TreatWarningsAsErrors 「 我需要比較propertyName字符串中指定的節點的值。c#數組的長度始終爲0

public string CheckSettings(XElement propertyGroup, string groupName, string propertyName) 
    { 
     var setting = (from doc in propertyGroup?.Descendants(propertyName) select doc).ToArray(); 

     if (setting.Length == 0) 
     { 
      return groupName + ": " + propertyName + " is missing"; 
     } 

     var allOk = setting.All(n => n.Value.Equals("true", StringComparison.InvariantCultureIgnoreCase)); 
     return allOk ? null : groupName + ": " + propertyName + " has wrong state."; 
    } 

示例XML

<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> 
<DebugSymbols>true</DebugSymbols> 
<DebugType>full</DebugType> 
<Optimize>false</Optimize> 
<OutputPath>..\..\..\..\..\..\Bin\AlfaStandardXmlManifest\</OutputPath> 
<DefineConstants>DEBUG;TRACE</DefineConstants> 
<ErrorReport>prompt</ErrorReport> 
<WarningLevel>4</WarningLevel> 
<UseVSHostingProcess>false</UseVSHostingProcess> 
<DocumentationFile>..\..\..\..\..\..\Bin\AlfaStandardXmlManifest\AlfaStandardXmlManifest.XML</DocumentationFile> 
<CodeAnalysisRuleSet>..\..\..\..\Build\FxCopSoftship_ZeroTolerance.ruleset</CodeAnalysisRuleSet> 
<RunCodeAnalysis>false</RunCodeAnalysis> 
<TreatWarningsAsErrors>false</TreatWarningsAsErrors> 
</PropertyGroup> 

添加了XML負載和settigns查詢電話:

var xmlDoc = XDocument.Load(projectFilePath); 
     XNamespace nameSpace = xmlDoc.Root?.Name.Namespace; 
     if (xmlDoc.Root != null) 
     { 
      var groups = xmlDoc.Root.Descendants(nameSpace + "PropertyGroup").ToArray(); 

      foreach (var group in groups) 
      { 
       result.Add(CheckSettings(group, GroupName(group), "RunCodeAnalysis")); 
       result.Add(CheckSettings(group, GroupName(group), "TreatWarningsAsErrors")); 

下面是調試器給了我組

<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' " xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> 
    <DebugSymbols>true</DebugSymbols> 
    <DebugType>full</DebugType> 
    <Optimize>false</Optimize> 
    <OutputPath>..\..\..\..\..\..\Bin\AlfaStandardXmlManifest\</OutputPath> 
    <DefineConstants>DEBUG;TRACE</DefineConstants> 
    <ErrorReport>prompt</ErrorReport> 
    <WarningLevel>4</WarningLevel> 
    <UseVSHostingProcess>false</UseVSHostingProcess> 
    <DocumentationFile>..\..\..\..\..\..\Bin\AlfaStandardXmlManifest\AlfaStandardXmlManifest.XML</DocumentationFile> 
    <CodeAnalysisRuleSet>..\..\..\..\Build\FxCopSoftship_ZeroTolerance.ruleset</CodeAnalysisRuleSet> 
    <RunCodeAnalysis>false</RunCodeAnalysis> 
    <TreatWarningsAsErrors>false</TreatWarningsAsErrors> 
</PropertyGroup> 
+2

'ToArray()'永遠不會返回null。究竟是什麼問題? – SLaks

+0

@SLaks代碼似乎總是跳入if語句中的返回內部,它不應該是 – Pagodatree

+3

這不同於它返回null,是嗎?這意味着沒有結果,這有點不同。我猜你的查詢是不正確的,這個名字可能有一個非默認的命名空間。你需要提供[mcve]。 –

回答

0

好吧,這個問題,像SLaks說,是與命名空間。在您的CheckSettings中,在Descendants內部,您缺少命名空間:

XNamespace nameSpace = propertyGroup.Name.Namespace; 
var setting = (from doc in propertyGroup?.Descendants(nameSpace + propertyName) select doc).ToArray(); 
+0

組中的數據,這個作品最後非常感謝你 – Pagodatree

2

大多數的MSBuild項目文件具有XML名稱空間(根元素中的xmlns="..."),它適用於XML中的所有名稱。

你需要在你的元素名稱這個命名空間:

XNamespace ns = "..."; 
XName name = ns + "...";