c# - XML validation with XSD getting invalid child element error and I have no idea why? -
using following invalid child element error. new xml , have been looking around net try , figure out have had no luck. have xsd verifying xml submitted application , works wonderfully using attributes instead of elements. can't work using elements in xsd validate xml being submitted through 3rd party application 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 not declared")) { return; } interrcount++; return; }
at least 1 problem ordering entry child elements incorrectly. elements defined in sequence tag must appear in corresponding xml doc(s) in same order.
after validating sample xml against schema issue saw.
edit:
if have no control on input file's element order & not consistent & each child element of entry can appear maximum of once per entry, may want use all element instead of sequence.
Comments
Post a Comment