Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XML validation with XSD getting invalid child element error and I have no idea why?

Tags:

c#

xml

xsd

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) { 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;
    }

like image 393
Brian Avatar asked Jan 16 '23 12:01

Brian


1 Answers

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.

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

EDIT:

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.

like image 174
Mark M Avatar answered Feb 16 '23 17:02

Mark M