c# - validating XML node over a XSD file -


i'm trying validate xml nodes or fragments against xml schema. have read article:

validating xml nodes, not entire document

but chosen solution doesn't work me.

private void validatesubnode(xmlnode node, xmlschema schema) {   xmltextreader reader = new xmltextreader(node.outerxml, xmlnodetype.element, null);    xmlreadersettings settings = new xmlreadersettings();   settings.conformancelevel = conformancelevel.fragment;   settings.schemas.add(schema);   settings.validationtype = validationtype.schema;   settings.validationeventhandler += new validationeventhandler(xsdvalidationeventhandler);    xmlreader validationreader = xmlreader.create(reader, settings);    while (validationreader.read())   {   } }  private void xsdvalidationeventhandler(object sender, validationeventargs args) {   errors.appendformat("xsd - severity {0} - {1}",                    args.severity.tostring(), args.message); } 

wich is, far can see, code validate full document, "conformancelevel.fragment"

thus, example, having schema simple this:

<xsd:schema xmlns:xsd='http://www.w3.org/2001/xmlschema'> <xsd:element name="customer">   <xsd:complextype>     <xsd:sequence>       <xsd:element name="address">         <xsd:complextype>           <xsd:sequence>             <xsd:element name="line1" type="xsd:string" />             <xsd:element name="line2" type="xsd:string" />           </xsd:sequence>        </xsd:complextype>       </xsd:element>     </xsd:sequence>   </xsd:complextype> </xsd:element> </xsd:schema> 

a 'root' node validates ok

<customer>   <address>     <line1>foo</line1>     <line2>foo2</line2>   </address> </customer> 

but inner node doesn't validate

  <address>     <line1>foo</line1>     <line2>foo2</line2>   </address> 

i receive error: "'address' element not declared"

is there missing?

your schema not allow stand-alone address elements, , when pass validation fails.

modify schema this:

<xsd:schema xmlns:xsd='http://www.w3.org/2001/xmlschema'>   <xsd:element name="address">     <xsd:complextype>       <xsd:sequence>         <xsd:element name="line1" type="xsd:string" />         <xsd:element name="line2" type="xsd:string" />       </xsd:sequence>     </xsd:complextype>   </xsd:element>   <xsd:element name="customer">     <xsd:complextype>       <xsd:sequence>         <xsd:element ref="address"/>       </xsd:sequence>     </xsd:complextype>   </xsd:element> </xsd:schema> 

validation of fragments of xml xsd not work: same element can valid or not - or have different valid structures - depending on in document matches xsd, validating stand-alone element not (in general) possible.


Comments

Popular posts from this blog

c++ - CryptStringToBinary API behavior -

c++ - Correct method for redrawing a layered window -

java.util.scanner - How to read and add only numbers to array from a text file -