Validate XML with XSD - Tearing my hair out

  • Thread starter Thread starter daz_oldham
  • Start date Start date
D

daz_oldham

I've got a couple of good books here, read online, but I really am
having no luck whatsoever with this.

In my DLL I am creating some XML in an XmlDocument data type, and I
would then like to vaildate it against my XSD which is on the local
hard drive.

I have tried it this way:

String sXSDPath = sSchemaLoc + sSchemaName;
xdXML.Schemas.Add(null, sXSDPath);
ValidationEventHandler veh = new
ValidationEventHandler(ValidateXMLHandler);
xdXML.Validate(veh);

But when I step through, I don't seem to be ging into my
ValidateXMLHandler.

Is this the correct way to do it?

Many thanks

Darren
 
daz_oldham wrote:

String sXSDPath = sSchemaLoc + sSchemaName;
xdXML.Schemas.Add(null, sXSDPath);
ValidationEventHandler veh = new
ValidationEventHandler(ValidateXMLHandler);
xdXML.Validate(veh);

But when I step through, I don't seem to be ging into my
ValidateXMLHandler.

Does it help if you add

String sXSDPath = sSchemaLoc + sSchemaName;
xdXML.Schemas.Add(null, sXSDPath);
if (!xdXML.Schemas.IsCompiled) {
xdXML.Schemas.Compile();
}
ValidationEventHandler veh = new
ValidationEventHandler(ValidateXMLHandler);
xdXML.Validate(veh);
 
Hi Martin

Unfortunately not, it does complile the XSD, but then just steps over
my xdXML.Validate(veh);

I tried changing one of my nodes so that it should fail the validation,
but it didn't drop into it.

Am I doing the valiadtion in the correct manner?

Thanks

Daz
 
daz_oldham wrote:

I tried changing one of my nodes so that it should fail the validation,
but it didn't drop into it.

Am I doing the valiadtion in the correct manner?

I couldn't see anything wrong in your snippet of code.
Is that a complex schema? If not then post it or at least the relevant
lines.
Does it use namespaces and does your XML you create use namespace too?
 
My XSD does include other XSDs...
<xs:include schemaLocation="APP_CommonTypes.xsd"/>

Which in turn calls...
<xs:include schemaLocation="APP_SimpleTypes.xsd"/>

Which has a list of Simple Type definitions.

To give you an idea of what is going on, here's the content of my
initial XSD.

Many thanks

Darren

<xs:schema targetNamespace="http://www.mynamespace.com/app/2002/09"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns="http://www.mynamespace.com/app/2002/09"
elementFormDefault="qualified" version="1.0">
<xs:include schemaLocation="APP_CommonTypes.xsd"/>
<xs:element name="APP_AccomAvailRQ">
<xs:annotation>
<xs:documentation>The APP_AccomAvailRQ is used to find available
accommodation.</xs:documentation>
</xs:annotation>
<xs:complexType>
<xs:complexContent>
<xs:extension base="APPRequestResponse">
<xs:sequence>
<xs:choice>
<xs:element name="AccommodationSearchRequest"
type="AccommodationSearchRequest"/>
<xs:element name="ResultSetRequest" type="ResultSetRequest"/>
</xs:choice>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
</xs:element>
</xs:schema>
 
My XSD does include other XSDs...
<xs:include schemaLocation="APP_CommonTypes.xsd"/>

Which in turn calls...
<xs:include schemaLocation="APP_SimpleTypes.xsd"/>

Which has a list of Simple Type definitions.

To give you an idea of what is going on, here's the content of my
initial XSD.

Many thanks

Darren

<xs:schema targetNamespace="http://www.mynamespace.com/app/2002/09"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns="http://www.mynamespace.com/app/2002/09"
elementFormDefault="qualified" version="1.0">
<xs:include schemaLocation="APP_CommonTypes.xsd"/>
<xs:element name="APP_AccomAvailRQ">
<xs:annotation>
<xs:documentation>The APP_AccomAvailRQ is used to find available
accommodation.</xs:documentation>
</xs:annotation>
<xs:complexType>
<xs:complexContent>
<xs:extension base="APPRequestResponse">
<xs:sequence>
<xs:choice>
<xs:element name="AccommodationSearchRequest"
type="AccommodationSearchRequest"/>
<xs:element name="ResultSetRequest" type="ResultSetRequest"/>
</xs:choice>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
</xs:element>
</xs:schema>
 
daz_oldham wrote:

<xs:schema targetNamespace="http://www.mynamespace.com/app/2002/09"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns="http://www.mynamespace.com/app/2002/09"
elementFormDefault="qualified" version="1.0">
<xs:include schemaLocation="APP_CommonTypes.xsd"/>
<xs:element name="APP_AccomAvailRQ">

So your schema defines elements in the target namespace
http://www.mynamespace.com/app/2002/09. Do you properly create elements
in that namespace with the DOM e.g.
xmlDocumentInstance.CreateElement("APP_AccomAvailRQ",
"http://www.mynamespace.com/app/2002/09")
 
Yes Martin, I definately do that in my XML.

Currently, I have the below code which only importing my
APP_CommonTypes.xsd. Do I need to be importing all the others to an
object, and if so, how do I do this?

Many thanks

Darren

public Boolean ValidateXML(string sSchemaName, string sSchemaLoc,
XmlDocument xdXML)
{

String sXSDPath = sSchemaLoc + sSchemaName;
xdXML.Schemas.Add(null, sXSDPath);
if (!xdXML.Schemas.IsCompiled)
{
xdXML.Schemas.Compile();
}
ValidationEventHandler veh = new
ValidationEventHandler(ValidateXMLHandler);
xdXML.Validate(veh);




return true;

}
 
Back
Top