Re: Trouble validating XML w/ XSD.



This is the case where .NET 1.1 used to throw an error but .NET 2.0 now
throws only a warning.

For a root element node, if a schema matching the namespace if the element
is not found, then lax validation kicks in according to XSD spec. This
element is then validated according to the content model of xs:anyType,
which contains an xs:any with processContents=lax.

Since the declarations for 'thing' and 'thing1' are not found, it only
produces warnings, rather than errors, since this is how the semantics of
xs:any with processContents=lax work.

Note that in the invalid xml, your root element is in a namespace for which
you do not have a schema. If you remove the namespace declaration
xmlns="http://purl.org/dc/elements/1.1/"; and let <thing> be in the
targetNamespace of the schema, then you will see the errors.

Zafar


"Jonas Bush" <spicyj2k@xxxxxxxxx> wrote in message
news:1135109791.019979.3500@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
> I've got the some code to try and validate some xml. Against my schema,
> the "Good" xml (below) produces a couple of warnings, which I don't
> care about. The "Bad" xml (also below), produces warnings as well, but
> *should* be producing errors. The XML validator at
> http://apps.gotdotnet.com/xmltools/xsdvalidator/Default.aspx reports
> that the "Good" xml produces warnings, but the "bad" xml produces
> errors, which is what I want to reproduce in my code. The code and all
> the xml/xsd are below. Any help would be greatly appreciated!
>
> Jonas
> spicyj2k@xxxxxxxxx
>
> private static void TestValidation()
> {
> //string rss = @"c:\t1\po.xml";
> string rss = @"c:\t1\pobad.xml";
> string file = @"c:\t1\po.xsd";
>
> StreamReader mem = new StreamReader(rss, Encoding.Default);
> XmlReaderSettings settings = new XmlReaderSettings();
> settings.ValidationEventHandler += new
> ValidationEventHandler(valReader_ValidationEventHandler);
> settings.ValidationType = ValidationType.Schema;
> settings.XmlResolver = null;
> settings.ValidationFlags =
> XmlSchemaValidationFlags.ReportValidationWarnings;
> settings.Schemas.Add(null, file);
> XmlReader rdr = XmlReader.Create(mem, settings);
>
> while (rdr.Read())
> {
> }
>
> rdr.Close();
> mem.Close();
> }
>
>
> private static void valReader_ValidationEventHandler(object sender,
> ValidationEventArgs e)
> {
> if (e.Severity == XmlSeverityType.Error)
> {
> Console.WriteLine(e.Message);
> Console.WriteLine(e.Severity.ToString());
> }
> }
>
>
> "Invalid" XML:
> <?xml version="1.0"?>
> <thing xmlns="http://purl.org/dc/elements/1.1/";>
> <thing1>hello there!</thing1>
> </thing>
>
> XSD
>
> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema";>
> <xsd:element name="purchaseOrder" type="PurchaseOrderType"/>
> <xsd:element name="comment" type="xsd:string"/>
> <xsd:complexType name="PurchaseOrderType">
> <xsd:sequence>
> <xsd:element name="shipTo" type="USAddress"/>
> <xsd:element name="billTo" type="USAddress"/>
> <xsd:element ref="comment" minOccurs="0"/>
> </xsd:sequence>
> <xsd:attribute name="orderDate" type="xsd:date"/>
> </xsd:complexType>
> <xsd:complexType name="USAddress">
> <xsd:sequence>
> <xsd:element name="name" type="xsd:string"/>
> <xsd:element name="street" type="xsd:string"/>
> <xsd:element name="city" type="xsd:string"/>
> <xsd:element name="state" type="xsd:string"/>
> <xsd:element name="zip" type="xsd:decimal"/>
> <xsd:any namespace="##other" processContents="lax" minOccurs="0"
> maxOccurs="unbounded"/>
> </xsd:sequence>
> <xsd:attribute name="country" type="xsd:NMTOKEN"
> fixed="US"/>
> </xsd:complexType>
> </xsd:schema>
>
> "Valid" XML
> <?xml version="1.0"?>
> <purchaseOrder xmlns:dc="http://purl.org/dc/elements/1.1/";
> orderDate="1999-10-20">
> <shipTo country="US">
> <name>Alice Smith</name>
> <street>123 Maple Street</street>
> <city>Mill Valley</city>
> <state>CA</state>
> <zip>90952</zip>
> <dc:wtf>hello</dc:wtf>
> </shipTo>
> <billTo country="US">
> <name>Robert Smith</name>
> <street>8 Oak Avenue</street>
> <city>Old Town</city>
> <state>PA</state>
> <zip>95819</zip>
> </billTo>
> <comment>Hurry, my lawn is going wild!</comment>
> </purchaseOrder>
>


.


Quantcast