C# XML 无法根据 XmlReaderSettings 中的架构正确验证

Pat*_*k Q 5 c# xml validation schema xsd

我进行了搜索,没有发现任何解决此问题的问题。

我正在尝试根据模式验证各种 XML,它似乎正在验证所有格式良好的 XML,而不仅仅是符合模式的 XML。我已经发布了我正在使用的代码、架构、示例有效 XML 和示例无效 XML。

我已经为此苦苦挣扎了一段时间。我对这件事的大部分内容一无所知。我必须学习如何编写 XSD,编写 XSD,然后学习如何在 C# 中解析 XML。这些都是我以前从未做过的。我使用了许多教程和微软网站来得出以下内容。我认为这应该有效,但事实并非如此。

我究竟做错了什么?

private bool ValidateXmlAgainstSchema(string sourceXml, string schemaUri)
{
    bool validated = false;
    try
    {
        //  REF:
        //  This should create a SCHEMA-VALIDATING XMLREADER
        //  http://msdn.microsoft.com/en-us/library/w5aahf2a(v=vs.110).aspx

        XmlReaderSettings xmlSettings = new XmlReaderSettings();
        xmlSettings.Schemas.Add("MySchema.xsd", schemaUri);
        xmlSettings.ValidationType = ValidationType.Schema;
        xmlSettings.ValidationFlags = XmlSchemaValidationFlags.None;
        XmlReader xmlReader = XmlReader.Create(new StringReader(sourceXml), xmlSettings);

        //  parse the input (not sure this is needed)
        while (xmlReader.Read()) ;

        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(xmlReader);

        validated = true;
    }
    catch (XmlException e)
    {
        //  load or parse error in the XML
        validated = false;
    }
    catch (XmlSchemaValidationException e)
    {
        //  Validation failure in XML
        validated = false;
    }
    catch (Exception e)
    {
        validated = false;
    }
    return validated;
}
Run Code Online (Sandbox Code Playgroud)

XSD/架构。目的是接受包含事件或感兴趣的人员的 XML。

<?xml version="1.0" encoding="utf-8"?>
<xs:schema
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    targetNamespace="MySchema.xsd"
    xmlns="MySchema.xsd"
    elementFormDefault="qualified"
>
  <xs:element name="Incident" type="IncidentType"/>
  <xs:element name="PersonOfInterest" type="PersonOfInterestType"/>

  <xs:complexType name="IncidentType">
    <xs:sequence>
      <xs:element name="Description" type="xs:string" minOccurs="0" maxOccurs="unbounded"/>
      <xs:element name="PersonOfInterest" type="PersonOfInterestType" minOccurs="0" maxOccurs="unbounded"/>
    </xs:sequence>
  </xs:complexType>

  <xs:complexType name="PersonOfInterestType">
    <xs:sequence>
      <xs:element name="Name" type="xs:string" minOccurs="1" maxOccurs="1"/>
    </xs:sequence>
  </xs:complexType>

</xs:schema>
Run Code Online (Sandbox Code Playgroud)

这是有效XML的示例

<?xml version="1.0" encoding="utf-8" ?>

  <Incident
      xmlns="MySchema.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.w3schools.com MySchema.xsd"
  >
    <Description>something happened</Description>
    <PersonOfInterest>
        <Name>Joe</Name>
    </PersonOfInterest>
    <PersonOfInterest>
        <Name>Sue</Name>
    </PersonOfInterest>
  </Incident>
Run Code Online (Sandbox Code Playgroud)

这是格式良好的无效XML 的示例,它应该引发异常(我认为),但是当我尝试它时,代码返回 true,表明它对架构有效。

<ghost>Boo</ghost>
Run Code Online (Sandbox Code Playgroud)

tom*_*ern 5

您进行验​​证的原因<ghost>Boo</ghost>是解析器找不到任何与 xml 匹配的模式。如果没有模式,则解析器假定有效性,前提是 xml 格式良好。我知道这是违反直觉的,并且可能会根据解析器的实现而有所不同。

尽管如此,您的代码仍然存在几个问题:

两个根元素

这在 xsd 中是一个很大的禁忌——你只能有一个根元素。一些解析器实际上会抛出异常,另一些解析器会容忍它,但只会使用第一个根元素(在您的情况下为Incident)进行任何后续验证。

schemaLocation 属性的使用

该值应采用以下值:(namespace) (URI)命名空间是架构的 targetNamespace,URI 是架构的位置。在您的情况下,您似乎使用架构文件名作为目标命名空间。此外,查看您的代码,您正在将架构加载到 xml 读取器中,因此您实际上根本不需要 schemaLocation 属性。这是一个可选属性,一些解析器完全忽略它。

我建议进行以下更改:

<xs:schema
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    targetNamespace="http://MyMoreMeaningfulNamespace"
    xmlns="http://MyMoreMeaningfulNamespace"
    elementFormDefault="qualified"
>
  <xs:element name="Root">
    <xs:complexType>
      <xs:sequence>
        <xs:element maxOccurs="unbounded" name="Incident" type="IncidentType"/>
        <xs:element maxOccurs="unbounded" name="PersonOfInterest" type="PersonOfInterestType"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>

  <xs:complexType name="IncidentType">
    <xs:sequence>
      <xs:element name="Description" type="xs:string" minOccurs="0" maxOccurs="unbounded"/>
      <xs:element name="PersonOfInterest" type="PersonOfInterestType" minOccurs="0" maxOccurs="unbounded"/>
    </xs:sequence>
  </xs:complexType>

  <xs:complexType name="PersonOfInterestType">
    <xs:sequence>
      <xs:element name="Name" type="xs:string" minOccurs="1" maxOccurs="1"/>
    </xs:sequence>
  </xs:complexType>

</xs:schema>
Run Code Online (Sandbox Code Playgroud)

这验证了这个实例

<Root xmlns="http://MyMoreMeaningfulNamespace">
  <Incident>
    <Description>something happened</Description>
    <PersonOfInterest>
      <Name>Joe</Name>
    </PersonOfInterest>
    <PersonOfInterest>
      <Name>Sue</Name>
    </PersonOfInterest>
  </Incident>

  <Incident>
    ...
  </Incident>

  <PersonOfInterest>
    <Name>Manny</Name>
  </PersonOfInterest>

  <PersonOfInterest>
    ...
  </PersonOfInterest> 

</Root>
Run Code Online (Sandbox Code Playgroud)