XML 反对 XSD 来自使用 XDocument 的具有 MinOccurance 的文件

XML against XSD from file with MinOccurance using XDocument

我使用一些 xsdMarkup

   string xsdMarkup =
        @"<?xml version='1.0' encoding='utf-8'?>
    <xs:schema attributeFormDefault='unqualified' elementFormDefault='qualified' xmlns:xs='http://www.w3.org/2001/XMLSchema'>
      <xs:element name='catalog'>
        <xs:complexType>
          <xs:sequence>
            <xs:element maxOccurs='unbounded' name='book'>
              <xs:complexType>
                <xs:sequence>
                  <xs:element name='authors'>
                    <xs:complexType>
                      <xs:sequence>
                        <xs:element maxOccurs='unbounded' name='author' type='xs:string' />
                      </xs:sequence>
                    </xs:complexType>
                  </xs:element>
                  <xs:element name='title' type='xs:string' maxOccurs='1' minOccurs='1' />
                  <xs:element name='genre' type='xs:string' maxOccurs='1' minOccurs='1' />
                  <xs:element name='price' type='xs:string' maxOccurs='1' minOccurs='1'/>
                  <xs:element name='publish_date' type='xs:string' maxOccurs='1' />
                  <xs:element name='description' type='xs:string' maxOccurs='1' />
                </xs:sequence>
                <xs:attribute name='id' type='xs:unsignedByte' use='required' />
              </xs:complexType>
            </xs:element>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
    </xs:schema>";


 XmlSchemaSet schemas = new XmlSchemaSet();

下一个方法验证:

public string ValidateXml(XDocument doc)
    {
        string errors_text = "";
        bool errors = false;
        doc.Validate(schemas, (o, e) =>
        {
            MessageBox.Show(e.Message);
            errors = true;
            errors_text += e.Message;
        });

        Label1.Content = (errors == true) ? "nie przeszło" : "przeszło";

        return errors_text;
    }

打开文件并验证该文件:

 private void OpenFile_Click(object sender, RoutedEventArgs e)
        {
             schemas.Add("", XmlReader.Create(new StringReader(xsdMarkup)));
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Multiselect = false;
            openFileDialog.Filter = "XML files (*.XML)|*.XML|All files (*.*)|*.*";

            if (openFileDialog.ShowDialog() == true)
            {
               XDocument asd = XDocument.Load(openFileDialog.FileName);
               ValidateXml(asd);           
            }

但是如果我的文件缺少标题、类型、publish_date、价格,我没有收到任何错误,我想知道这是为什么?你能帮我解决这个问题吗??

这是我加载的示例 .xml 文件

<?xml version="1.0"?>
<catalog>
 <book  id="001">
  <authors>
   <author>P</author>
   <author>A</author>
  </authors>
  <title>XML Developer's Guide</title>
  <genre>Computer</genre>
  <price>44.95</price>
  <publish_date>2000-10-01</publish_date>
  <description>An in-depth look at creating applications with XML.</description>
 </book>
 <book  id="002">
  <authors>
   <author>K</author>
  </authors>
  <title>Midnight Rain</title>
  <genre>Fantasy</genre>
  <price>5.95</price>
  <publish_date>2000-12-16</publish_date>
  <description>A former architect battles corporate zombies, an evil sorceress, and her own childhood to become queen of the world.</description>
 </book>
 <book  id="003">
  <authors>
   <author></author>
  </authors>
  <title></title>
  <genre></genre>
  <price></price>
  <publish_date></publish_date>
  <description></description>
 </book>
 <book id="004">
  <authors>
   <author>asd</author>
  </authors>
  <title>asd</title>
  <genre>asdasd</genre>
  <price></price>
  <publish_date></publish_date>
  <description></description>
 </book>
</catalog>

我注意到您的 XML 文件在每种情况下都有 titlegenrepublish_date 的标签。只是其中一些是空的。

都是字符串,空字符串为有效字符串。所以(我假设)XML 解析器认为这些标签具有有效值。

您的架构指定的只是标签需要存在。它没有说明它们包含的字符串应该是什么样子。

如果您希望空标签被视为错误,您可能需要优化您的架构以包括对值的进一步限制(例如最小长度或价格的非字符串数据类型)。