473,396 Members | 1,891 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Trouble validating XML w/ XSD.

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>

Dec 20 '05 #1
4 1725
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" <sp******@gmail.com> wrote in message
news:11********************@g43g2000cwa.googlegrou ps.com...
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>

Dec 27 '05 #2
To answer the last part, I can't remove the namespace declaration from
the "invalid" xml because that's how it comes into my application. :)
How does the validator at GDN produce errors given the documents up
there?

Dec 28 '05 #3


Jonas Bush wrote:

How does the validator at GDN produce errors given the documents up
there?


As explained, .NET 1.x and .NET 2.0 are different for that case, and
that validator probably was and is still implemented with .NET 1.x.
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Dec 28 '05 #4
The validator you tried uses .NET 1.1 - the reason of the errors.

"Jonas Bush" <sp******@gmail.com> wrote in message
news:11*********************@g49g2000cwa.googlegro ups.com...
To answer the last part, I can't remove the namespace declaration from
the "invalid" xml because that's how it comes into my application. :)
How does the validator at GDN produce errors given the documents up
there?

Dec 28 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Group IIS | last post by:
Hi all, I am using ALTOVA XMLSpy. The start of my XML file looks like this: <?xml version="1.0" encoding="UTF-8" ?> <Submission xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"...
9
by: scott | last post by:
I have a style variable below that I can't figure correct quotes for in response.write line. Any help? CODE: sTeamVisible = "visibility: visible;" Response.Write "<td...
4
by: bibsoconner | last post by:
Hi, I hope someone can please help me. I'm having a lot of trouble with schema files in .NET. I have produced a very simple example that uses "include" to include other schema files. It all...
6
by: Alex Bink | last post by:
Hi, I have a validating event on a textbox in which I want to prevent the user to leave the textbox without entering the right data. Only if he clicks on another specific control he is allowed...
1
by: Buggyman | last post by:
Hi, I'm having trouble validating an intranet URL. I'm using the regular expresssion validator (though any ideas accepted), and using the standard Internet URL definition, which is.....
0
by: Joe | last post by:
Hi For a while now I have been finding postings of problems with the validating event not firing on controls properly. I too had this problem. The event would fire when clicking on another...
2
by: Chris Dunaway | last post by:
I have a form with a textbox and numerous panels, buttons and other controls. I have handled the textbox Validating and Validated events. The textbox will hold a filename. In the validating...
0
by: Gary Shell | last post by:
I am experiencing some strange behavior between a UserControl's validating event and a treeview control. Initially, I thought it was related to an issue in the Knowledgebase article 810852...
2
by: josh | last post by:
Hi, I am trying to validate cXML documents against cXML.dtd using the XmlValidatingReader. If I set the XMLValidatingReader's ValidatingType to ValidationType.DTD, I get the following...
0
by: Peted | last post by:
Hi, im having some trouble with reg expression pattern matching for something i think should be a straightforward test. Im validating the text being entered in a winforms textbox and i need...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.