设置的长度始终为0,因此代码跳转到if语句的返回,这不应该是,我无法弄清楚为什么,poperty被设置为“TreatWarningsAsErrors” 我需要比较propertyName string中指定的节点的值。
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>
答案 0 :(得分:2)
大多数MSBuild项目文件都有一个XML命名空间(根元素中为xmlns="..."
),它适用于XML中的所有名称。
您需要在元素名称中包含此命名空间:
XNamespace ns = "...";
XName name = ns + "...";
答案 1 :(得分:0)
好吧,像SLaks说的那样,问题在于命名空间。在CheckSettings中,在Descendants中你缺少命名空间:
XNamespace nameSpace = propertyGroup.Name.Namespace;
var setting = (from doc in propertyGroup?.Descendants(nameSpace + propertyName) select doc).ToArray();