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/x...r/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
sp******@gmail.com
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_ValidationEventHa ndler);
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>