473,950 Members | 61,277 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.x ml";
string file = @"c:\t1\po.xsd" ;

StreamReader mem = new StreamReader(rs s, Encoding.Defaul t);
XmlReaderSettin gs settings = new XmlReaderSettin gs();
settings.Valida tionEventHandle r += new
ValidationEvent Handler(valRead er_ValidationEv entHandler);
settings.Valida tionType = ValidationType. Schema;
settings.XmlRes olver = null;
settings.Valida tionFlags =
XmlSchemaValida tionFlags.Repor tValidationWarn ings;
settings.Schema s.Add(null, file);
XmlReader rdr = XmlReader.Creat e(mem, settings);

while (rdr.Read())
{
}

rdr.Close();
mem.Close();
}
private static void valReader_Valid ationEventHandl er(object sender,
ValidationEvent Args e)
{
if (e.Severity == XmlSeverityType .Error)
{
Console.WriteLi ne(e.Message);
Console.WriteLi ne(e.Severity.T oString());
}
}
"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="purchaseO rder" type="PurchaseO rderType"/>
<xsd:element name="comment" type="xsd:strin g"/>
<xsd:complexTyp e name="PurchaseO rderType">
<xsd:sequence >
<xsd:element name="shipTo" type="USAddress "/>
<xsd:element name="billTo" type="USAddress "/>
<xsd:element ref="comment" minOccurs="0"/>
</xsd:sequence>
<xsd:attribut e name="orderDate " type="xsd:date"/>
</xsd:complexType >
<xsd:complexTyp e name="USAddress ">
<xsd:sequence >
<xsd:element name="name" type="xsd:strin g"/>
<xsd:element name="street" type="xsd:strin g"/>
<xsd:element name="city" type="xsd:strin g"/>
<xsd:element name="state" type="xsd:strin g"/>
<xsd:element name="zip" type="xsd:decim al"/>
<xsd:any namespace="##ot her" processContents ="lax" minOccurs="0"
maxOccurs="unbo unded"/>
</xsd:sequence>
<xsd:attribut e name="country" type="xsd:NMTOK EN"
fixed="US"/>
</xsd:complexType >
</xsd:schema>

"Valid" XML
<?xml version="1.0"?>
<purchaseOrde r 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>Hurr y, my lawn is going wild!</comment>
</purchaseOrder>

Dec 20 '05 #1
4 1749
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******** ************@g4 3g2000cwa.googl egroups.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.x ml";
string file = @"c:\t1\po.xsd" ;

StreamReader mem = new StreamReader(rs s, Encoding.Defaul t);
XmlReaderSettin gs settings = new XmlReaderSettin gs();
settings.Valida tionEventHandle r += new
ValidationEvent Handler(valRead er_ValidationEv entHandler);
settings.Valida tionType = ValidationType. Schema;
settings.XmlRes olver = null;
settings.Valida tionFlags =
XmlSchemaValida tionFlags.Repor tValidationWarn ings;
settings.Schema s.Add(null, file);
XmlReader rdr = XmlReader.Creat e(mem, settings);

while (rdr.Read())
{
}

rdr.Close();
mem.Close();
}
private static void valReader_Valid ationEventHandl er(object sender,
ValidationEvent Args e)
{
if (e.Severity == XmlSeverityType .Error)
{
Console.WriteLi ne(e.Message);
Console.WriteLi ne(e.Severity.T oString());
}
}
"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="purchaseO rder" type="PurchaseO rderType"/>
<xsd:element name="comment" type="xsd:strin g"/>
<xsd:complexTyp e name="PurchaseO rderType">
<xsd:sequence >
<xsd:element name="shipTo" type="USAddress "/>
<xsd:element name="billTo" type="USAddress "/>
<xsd:element ref="comment" minOccurs="0"/>
</xsd:sequence>
<xsd:attribut e name="orderDate " type="xsd:date"/>
</xsd:complexType >
<xsd:complexTyp e name="USAddress ">
<xsd:sequence >
<xsd:element name="name" type="xsd:strin g"/>
<xsd:element name="street" type="xsd:strin g"/>
<xsd:element name="city" type="xsd:strin g"/>
<xsd:element name="state" type="xsd:strin g"/>
<xsd:element name="zip" type="xsd:decim al"/>
<xsd:any namespace="##ot her" processContents ="lax" minOccurs="0"
maxOccurs="unbo unded"/>
</xsd:sequence>
<xsd:attribut e name="country" type="xsd:NMTOK EN"
fixed="US"/>
</xsd:complexType >
</xsd:schema>

"Valid" XML
<?xml version="1.0"?>
<purchaseOrde r 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>Hurr y, 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******** *************@g 49g2000cwa.goog legroups.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
2496
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" xsi:noNamespaceSchemaLocation="layout.xsd"> When I go to validate my XML file it give me error "This file is not
9
1404
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 class=""teamdrop1"" style=""" & sTeamVisible & """ & " "
4
2303
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 works with SPY, but when I pick "Validate Schema" from the .NET 2003 menu, it fails with message: "Type XType is not declared." As I hinted at in my subject line, I suspect that it has to do with including another schema multiple times.
6
5814
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 to leave the textbox without entering the right information. Is there a way to determine which other control was clicked in the validating event of the textbox? Thanks
1
1417
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.. http://(+\.)++(/*)? unfortunately this doesn't work with our intranet values, such as (e.g.) http://homer/wibble/viewDocument.aspx?Data=1789
0
1560
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 control which had it's causes validation property set to true however if I tabbed on to this control the event wouldn't fire. So after playing around with my code I figured out how to get it to work. I am not sure what the reason behind it is but it...
2
2144
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 event, I check that the string in the textbox is a file that exists or whether or not the string is blank and display a message box in either case. I also call e.Cancel so that the value will be corrected. However, certain buttons on the form...
0
2447
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 (http://support.microsoft.com/kb/810852), but then I realized that the hotfix mentioned was in .Net v1.1, which I am using. I took the sample from that article and recreated the situation I see in my application. (Code included below.) If you run the...
2
7212
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 System.Xml.Schema.XmlSchemaException: "The parameter entity replacement text must nest properly within markup
0
965
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 to bring up a message in a validation test if these patterns are found in the text enetered in the text box.
0
10172
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9992
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11602
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
11371
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10703
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9904
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8269
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7444
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6353
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.