Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/5.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# 使用.NET 4.6验证xml时出现NullReferenceException_C#_.net_Xsd - Fatal编程技术网

C# 使用.NET 4.6验证xml时出现NullReferenceException

C# 使用.NET 4.6验证xml时出现NullReferenceException,c#,.net,xsd,C#,.net,Xsd,我正在测试从.NET版本4.5.1切换到4.6,在对可选属性使用唯一约束时,在xsd验证中遇到NullReferenceException at System.Xml.Schema.KeySequence.ToString() at System.Xml.Schema.XmlSchemaValidator.EndElementIdentityConstraints(Object typedValue, String stringValue, XmlSchemaDatatype datatype)

我正在测试从.NET版本4.5.1切换到4.6,在对可选属性使用唯一约束时,在xsd验证中遇到NullReferenceException

at System.Xml.Schema.KeySequence.ToString()
at System.Xml.Schema.XmlSchemaValidator.EndElementIdentityConstraints(Object typedValue, String stringValue, XmlSchemaDatatype datatype)
at System.Xml.Schema.XmlSchemaValidator.InternalValidateEndElement(XmlSchemaInfo schemaInfo, Object typedValue)
at System.Xml.XsdValidatingReader.ProcessEndElementEvent()
at System.Xml.XsdValidatingReader.ProcessElementEvent()
at System.Xml.XsdValidatingReader.ProcessReaderEvent()
at System.Xml.XsdValidatingReader.Read()
at ConsoleApplication.Program.Main(String[] args)
这是一段剥离的代码,在以v4.5.x为目标时运行,但在使用4.6时失败并出现NullReferenceException。(使用VS2013和VS2015在Win7上测试)。这在xml中合法吗?即使没有,它也会引发一些XmlException

模式:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="Enumerations">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="Enum" minOccurs="0" maxOccurs="unbounded">
          <xs:complexType>
            <xs:attribute name="id" type="xs:string" use="optional"/>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
    <xs:unique name="unique_EnumId_contraint">
      <xs:selector xpath="Enum"/>
      <xs:field xpath="@id"/>
    </xs:unique>
  </xs:element>
</xs:schema>

我可以复制这个。在我看来像是个bug(
.NET4.6有很多…
)。你应该向我报告

虽然这是固定的,但您可以在此处检查源:

它告诉我们,它可以通过使用开关来克服,所以只需在任何其他代码之前添加此代码,它就会工作:

AppContext.SetSwitch("Switch.System.Xml.IgnoreEmptyKeySequences", true);
有关此开关的更多信息,请参见:-注意:“此更改的影响应最小”:-)


PS:我相信您也可以使用适当的.config文件来更改这些开关。

看起来枚举节点的id不是为了与以前的格式保持向后兼容性。我可以检查代码中的唯一性,但它应该像在4.5.xThanks中一样工作。我不知道MS还在更改XML解析。另一个问题针对另一个框架版本。在我的例子中,相同的代码是使用此开关的另一个默认值运行的,还是执行了旧代码?不,它仍然是.NET 4.6,但它采用了另一个路径(可能是旧代码路径,可能不是)。
var settings = new XmlReaderSettings();
settings.ValidationType = ValidationType.Schema;
settings.Schemas.Add(null, "enumerations.xsd");

using (var xmlReader = XmlReader.Create("enumerations.xml", settings))
{
    while (xmlReader.Read())
    {
        if (xmlReader.NodeType == XmlNodeType.Element)
        {
            Console.CursorLeft = xmlReader.Depth * 4;
            Console.WriteLine(xmlReader.Name);
        }
    }
}
AppContext.SetSwitch("Switch.System.Xml.IgnoreEmptyKeySequences", true);