Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
c#数组的长度始终为0_C#_Xml - Fatal编程技术网

c#数组的长度始终为0

c#数组的长度始终为0,c#,xml,C#,Xml,设置的长度始终为0,因此代码跳转到if语句的返回处,该语句不应为0,我不知道为什么,poperty设置为“treatwarningaserrors” 我需要比较propertyName字符串中指定的节点的值 public string CheckSettings(XElement propertyGroup, string groupName, string propertyName) { var setting = (from doc in propertyGroup?

设置的长度始终为0,因此代码跳转到if语句的返回处,该语句不应为0,我不知道为什么,poperty设置为“treatwarningaserrors” 我需要比较propertyName字符串中指定的节点的值

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>
下面是调试器为组提供的内容

    <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>

真的
满的
假的
..\..\..\..\..\..\Bin\AlfaStandardXmlManifest\
调试;痕迹
促使
4.
假的
..\..\..\..\..\..\..\Bin\AlfaStandardXmlManifest\AlfaStandardXmlManifest.XML
..\..\..\..\Build\FxCopSoftship\u ZeroTolerance.ruleset
假的
假的

大多数MSBuild项目文件在根元素中都有一个XML名称空间(
xmlns=“…”
),它适用于XML中的所有名称

您需要在元素名称中包含此命名空间:

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

大多数MSBuild项目文件都有一个XML命名空间(
xmlns=“…”
,位于根元素中),该命名空间适用于XML中的所有名称

您需要在元素名称中包含此命名空间:

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

好的,正如SLaks所说,问题在于名称空间。在CheckSettings中,在子体中缺少名称空间:

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

好的,正如SLaks所说,问题在于名称空间。在CheckSettings中,在子体中缺少名称空间:

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

ToArray()
永远不能返回null。到底是什么问题?@SLaks代码似乎总是跳入if语句中的返回,它不应该跳入if语句中。这与返回null不同,是吗?这意味着没有结果,这有点不同。我猜您的查询不正确,该名称可能有一个非默认名称空间。您需要提供一个。显然,subjects()可能不是您想要的,因为它将所有作为给定节点的子节点以及这些子节点的子节点返回给给定节点。。。您可能想使用Nodes()来代替,我编辑了这个问题以消除任何混淆
ToArray()
永远不能返回null。到底是什么问题?@SLaks代码似乎总是跳入if语句中的返回,它不应该跳入if语句中。这与返回null不同,是吗?这意味着没有结果,这有点不同。我猜您的查询不正确,该名称可能有一个非默认名称空间。您需要提供一个。显然,subjects()可能不是您想要的,因为它将所有作为给定节点的子节点以及这些子节点的子节点返回给给定节点。。。您可能想使用Nodes()来代替我编辑的问题,以消除任何混淆