使用 XSD 进行 XML 验证时出现无效子元素错误,我不知道为什么? [英] XML validation with XSD getting invalid child element error and I have no idea why?

查看:29
本文介绍了使用 XSD 进行 XML 验证时出现无效子元素错误,我不知道为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用以下我总是得到无效的子元素错误.我是 XML 的新手,我一直在网上寻找并试图解决这个问题,但没有运气.我有另一个 XSD 正在验证提交给我的应用程序的 XML,它工作得很好,但它使用的是属性而不是元素.无法使用 XSD 中的元素来验证通过我无法控制的第 3 方应用程序提交的 XML.

Using the following I always get an invalid child element error. I am new to XML and I have been looking around the net to try and figure this out but have had no luck. I have another XSD that is verifying XML submitted to my application and it works wonderfully but it is using attributes instead of elements. Can't get this to work using the elements in the XSD to validate XML being submitted through a 3rd party application that I have no control over.

XSD

<?xml version="1.0" encoding="Windows-1252"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified"     xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="SCCAParticipationList">
    <xs:complexType>
      <xs:sequence>
        <xs:element maxOccurs="unbounded" name="Entry">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="Address" type="xs:string" minOccurs="0" />
              <xs:element name="CarModel" type="xs:string" minOccurs="0" />
              <xs:element name="CarNo" type="xs:string" minOccurs="0" />
              <xs:element name="TotalTm" type="xs:string" minOccurs="0" />
              <xs:element name="BestTm" type="xs:string" minOccurs="0" />
              <xs:element name="Region" type="xs:string" minOccurs="0" />
              <xs:element name="MemberNo" type="xs:string" minOccurs="1" />
              <xs:element name="FirstName" type="xs:string" minOccurs="1" />
              <xs:element name="LastName" type="xs:string" minOccurs="1" />
              <xs:element name="Class" type="xs:string" minOccurs="1" />
              <xs:element name="Pos" type="xs:string" minOccurs="1" />
              <xs:element name="UniqueID" type="xs:string" minOccurs="0" />
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

XML

<?xml version="1.0"?>
<SCCAParticipationList>
  <Entry>
    <MemberNo>3333333</MemberNo>
    <FirstName>Test</FirstName>
    <LastName>Person</LastName>
    <Class>stt</Class>
    <Pos>13</Pos>
    <CarModel>Mazda Miata</CarModel>
    <Address>123 Test Dr ,The Woodlands TX,55555,US</Address>
  </Entry>
  <Entry>
    <MemberNo>2222222</MemberNo>
    <FirstName>John</FirstName>
    <LastName>Doe</LastName>
    <Class>sio</Class>
    <Pos>3T</Pos>
    <CarModel>Subaru Impreza</CarModel>
    <Address>111 Test Circle ,Austin TX,77777,US</Address>
  </Entry>
</SCCAParticipationList>

C#

protected Boolean VerifyXmlwElements(string strSchemaPath, string strXml){尝试{
byte[] byteArray = Encoding.ASCII.GetBytes(strXml);MemoryStream stream = new MemoryStream(byteArray);XmlTextReader xmlr = new XmlTextReader(stream);XmlValidatingReader xmlvread = 新 XmlValidatingReader(xmlr);xmlvread.Schemas.Add(null, strSchemaPath);

protected Boolean VerifyXmlwElements(string strSchemaPath, string strXml) { try {
byte[] byteArray = Encoding.ASCII.GetBytes(strXml); MemoryStream stream = new MemoryStream(byteArray); XmlTextReader xmlr = new XmlTextReader(stream); XmlValidatingReader xmlvread = new XmlValidatingReader(xmlr); xmlvread.Schemas.Add(null, strSchemaPath);

            xmlvread.ValidationEventHandler += new ValidationEventHandler(ValidationCallBack);

            while (xmlvread.Read()) { }

            xmlvread.Close();

            if (intErrCount > 0)
            {
                intErrCount--;
                throw new Exception(strErrMessage);
            }
            strErrMessage = "XML validation succeeded!\r\n";
            return true;
        }
        catch (Exception ex)
        {
            intErrCount++;
            strErrMessage = "Invalid XML - " + ex.Message + intErrCount.ToString() + " Error(s)\r\n";
            return false;
        }
    }

    private void ValidationCallBack(Object sender, ValidationEventArgs args)
    {
        if (args.Message.ToLower().Contains("attribute is not declared"))
        {
            return;
        }
        intErrCount++;
        return;
    }

推荐答案

至少有一个问题是您对 Entry 子元素的排序不正确.序列标签中定义的元素必须以相同的顺序出现在相应的 XML 文档中.

At least one problem is that you are ordering your Entry child elements incorrectly. Elements defined in a sequence tag must appear in the corresponding XML doc(s) in the same order.

根据您的架构验证示例 XML 后,这是我看到的唯一问题.

After validating your sample XML against your schema this was the only issue I saw.

如果您无法控制输入文件的元素顺序 &它不一致 &Entry 的每个子元素每个 Entry 最多只能出现一次,您可能需要使用 all 元素而不是序列.

If you have no control over the input file's element order & it is not consistent & each child element of Entry can only appear a maximum of once per Entry, you may want to use the all element instead of sequence.

这篇关于使用 XSD 进行 XML 验证时出现无效子元素错误,我不知道为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆