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

brookslaser

New Member
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 \[code\]\[code\]<?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>\[/code\]\[/code\]XML \[code\]\[code\]<?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>\[/code\]\[/code\]C# \[code\]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);\[code\] 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; }\[/code\]\[/code\]
 
Back
Top