設置的長度始終爲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>
'ToArray()'永遠不會返回null。究竟是什麼問題? – SLaks
@SLaks代碼似乎總是跳入if語句中的返回內部,它不應該是 – Pagodatree
這不同於它返回null,是嗎?這意味着沒有結果,這有點不同。我猜你的查詢是不正確的,這個名字可能有一個非默認的命名空間。你需要提供[mcve]。 –