473,386 Members | 1,799 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,386 software developers and data experts.

XMLSchemaCollection Validation with xs:choice xs:any elements

I have implemented the xmlSchemaCollection object to cache commonly
used schemas, as follows:

================================================== =============================

public void SchemaValidate(XmlDocument xmlDoc, string schemaURI,
string urnNamespace)
{
if (!m_SchemaCollection.Contains(urnNamespace))
{
//add new schema to collection cache if appropriate
m_SchemaCollection.Add(urnNamespace,schemaURI);
}

//set up validating reader
XmlValidatingReader reader = new XmlValidatingReader(
xmlDoc.OuterXml,
XmlNodeType.Document,
null);

reader.Namespaces = true;
reader.Schemas.Add(m_SchemaCollection);
reader.ValidationType = ValidationType.Schema;

ValidationEventHandler EvtHandler = new
ValidationEventHandler(this.ValidationCallBack);
reader.ValidationEventHandler += EvtHandler;

while (reader.Read())
{
//iterate entire document,building up XML Doc of ValidationErrors
}
if (m_ValErrorsXmlDoc.HasChildNodes)
{
#if DEBUG
m_ValErrorsXmlDoc.Save("C:\\temp\\debug\\SchemaVal idationErrors.xml");
#endif
throw new Exception("Schema Validation Errors:" +
Environment.NewLine + schemaURI + Environment.NewLine +
m_ValErrorsXmlDoc.OuterXml);
}
}

================================================== =============================

The caching seems to work fine, but I get validation errors on xs:any
elements. The reader complains that elements contained are not
declared..
I suspect I will have problems with xs:choice elements too.

Using the validating reader without the schema collection, the input
validates OK .. ie. as follows:

================================================== =============================

public XmlDocument SchemaValidate(string XML, string RulesSchema)
{

XmlValidatingReader reader = new XmlValidatingReader(
XML,
XmlNodeType.Document,
null);

XmlReader schemaReader = new XmlTextReader(RulesSchema);

XmlSchema schema = XmlSchema.Read(schemaReader, new
ValidationEventHandler(ValidationCallBack));

reader.Schemas.Add(schema);

while (reader.Read())
{
//iterate entire document,building up XML Doc of ValidationErrors
}

XmlDocument xml = new XmlDocument();

xml.Load(reader);
//xml.LoadXml(RulesXML);

return xml;
}
================================================== =============================

I have a vague idea that this is related to serialization of the
cached schemas? but am just guessing. I have spent a day on this
already...

Any help would be much appreciated.

TIA

Matt
Nov 12 '05 #1
2 2331
Caching the XmlSchemaCollection and using this for validation should be no
different than adding the schemas directly to the XmlValidatingReader except
for an improvement in performance.

Can you send the schema and the xml samples?

Thanks,
Priya

"Matthew" <ma****************@hotmail.com> wrote in message
news:4f**************************@posting.google.c om...
I have implemented the xmlSchemaCollection object to cache commonly
used schemas, as follows:

================================================== ==========================
===
public void SchemaValidate(XmlDocument xmlDoc, string schemaURI,
string urnNamespace)
{
if (!m_SchemaCollection.Contains(urnNamespace))
{
//add new schema to collection cache if appropriate
m_SchemaCollection.Add(urnNamespace,schemaURI);
}

//set up validating reader
XmlValidatingReader reader = new XmlValidatingReader(
xmlDoc.OuterXml,
XmlNodeType.Document,
null);

reader.Namespaces = true;
reader.Schemas.Add(m_SchemaCollection);
reader.ValidationType = ValidationType.Schema;

ValidationEventHandler EvtHandler = new
ValidationEventHandler(this.ValidationCallBack);
reader.ValidationEventHandler += EvtHandler;

while (reader.Read())
{
//iterate entire document,building up XML Doc of ValidationErrors
}
if (m_ValErrorsXmlDoc.HasChildNodes)
{
#if DEBUG
m_ValErrorsXmlDoc.Save("C:\\temp\\debug\\SchemaVal idationErrors.xml");
#endif
throw new Exception("Schema Validation Errors:" +
Environment.NewLine + schemaURI + Environment.NewLine +
m_ValErrorsXmlDoc.OuterXml);
}
}

================================================== ==========================
===
The caching seems to work fine, but I get validation errors on xs:any
elements. The reader complains that elements contained are not
declared..
I suspect I will have problems with xs:choice elements too.

Using the validating reader without the schema collection, the input
validates OK .. ie. as follows:

================================================== ==========================
===
public XmlDocument SchemaValidate(string XML, string RulesSchema)
{

XmlValidatingReader reader = new XmlValidatingReader(
XML,
XmlNodeType.Document,
null);

XmlReader schemaReader = new XmlTextReader(RulesSchema);

XmlSchema schema = XmlSchema.Read(schemaReader, new
ValidationEventHandler(ValidationCallBack));

reader.Schemas.Add(schema);

while (reader.Read())
{
//iterate entire document,building up XML Doc of ValidationErrors
}

XmlDocument xml = new XmlDocument();

xml.Load(reader);
//xml.LoadXml(RulesXML);

return xml;
}
================================================== ==========================
===
I have a vague idea that this is related to serialization of the
cached schemas? but am just guessing. I have spent a day on this
already...

Any help would be much appreciated.

TIA

Matt

Nov 12 '05 #2
Thanks Priyal,

I sent some source code to the online.microsoft.com address. But the
address could not be resolved. So I used a plain microsoft.com address
which went through, but I am uncertain whether or not you actually
received the documentation? Could you confirm your address for me?

Thanks,
Matthew

"Priya Lakshminarayanan [MSFT]" <pr****@online.microsoft.com> wrote in message news:<41********@news.microsoft.com>...
Caching the XmlSchemaCollection and using this for validation should be no
different than adding the schemas directly to the XmlValidatingReader except
for an improvement in performance.

Can you send the schema and the xml samples?

Thanks,
Priya

"Matthew" <ma****************@hotmail.com> wrote in message
news:4f**************************@posting.google.c om...
I have implemented the xmlSchemaCollection object to cache commonly
used schemas, as follows:

================================================== ==========================
===

public void SchemaValidate(XmlDocument xmlDoc, string schemaURI,
string urnNamespace)
{
if (!m_SchemaCollection.Contains(urnNamespace))
{
//add new schema to collection cache if appropriate
m_SchemaCollection.Add(urnNamespace,schemaURI);
}

//set up validating reader
XmlValidatingReader reader = new XmlValidatingReader(
xmlDoc.OuterXml,
XmlNodeType.Document,
null);

reader.Namespaces = true;
reader.Schemas.Add(m_SchemaCollection);
reader.ValidationType = ValidationType.Schema;

ValidationEventHandler EvtHandler = new
ValidationEventHandler(this.ValidationCallBack);
reader.ValidationEventHandler += EvtHandler;

while (reader.Read())
{
//iterate entire document,building up XML Doc of ValidationErrors
}
if (m_ValErrorsXmlDoc.HasChildNodes)
{
#if DEBUG
m_ValErrorsXmlDoc.Save("C:\\temp\\debug\\SchemaVal idationErrors.xml");
#endif
throw new Exception("Schema Validation Errors:" +
Environment.NewLine + schemaURI + Environment.NewLine +
m_ValErrorsXmlDoc.OuterXml);
}
}

================================================== ==========================
===

The caching seems to work fine, but I get validation errors on xs:any
elements. The reader complains that elements contained are not
declared..
I suspect I will have problems with xs:choice elements too.

Using the validating reader without the schema collection, the input
validates OK .. ie. as follows:

================================================== ==========================
===

public XmlDocument SchemaValidate(string XML, string RulesSchema)
{

XmlValidatingReader reader = new XmlValidatingReader(
XML,
XmlNodeType.Document,
null);

XmlReader schemaReader = new XmlTextReader(RulesSchema);

XmlSchema schema = XmlSchema.Read(schemaReader, new
ValidationEventHandler(ValidationCallBack));

reader.Schemas.Add(schema);

while (reader.Read())
{
//iterate entire document,building up XML Doc of ValidationErrors
}

XmlDocument xml = new XmlDocument();

xml.Load(reader);
//xml.LoadXml(RulesXML);

return xml;
}

================================================== ==========================
===

I have a vague idea that this is related to serialization of the
cached schemas? but am just guessing. I have spent a day on this
already...

Any help would be much appreciated.

TIA

Matt

Nov 12 '05 #3

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

Similar topics

2
by: Clarence | last post by:
Hi, here is an XML schema excerpt for an employees details. <xs:element name="ADDRESS"> <xs:complexType> <xs:sequence> <xs:element name="ADDRESSLINE" type="xs:string" maxOccurs="6"/>...
2
by: Tjerk Wolterink | last post by:
I've a element definition like this: -- <xs:element name="content"> <xs:complexType> <xs:sequence> <xs:choice minOccurs="0" maxOccurs="unbounded"> <xs:any...
1
by: Oleg Ogurok | last post by:
Hi all, I have a complex type defined as follows: <xs:complexType name="SchedulingMethodType"> <xs:choice maxOccurs="1"> <xs:element name="Interval" type="xs:duration" /> <xs:element...
0
by: Chris Kelly | last post by:
I am trying to specify an xsd schema for a dataset (using dataset.readxmlschema), before filling the dataset from an xml document (using dataset.readxml), in order to define relationships between...
4
by: Sergey Poberezovskiy | last post by:
Hi, As part of my schema I need to ensure that at least one of two fields have values. I defined my schema as follows: .... <xs:choice> <xs:element ref="el1"/> <xs:element ref="el2"/>...
2
by: Sergey Poberezovskiy | last post by:
Hi, If I define my schema as <xs:choice> <xs:sequence> <xs:element ref="el_1" /> <xs:element ref="el_2" /> </xs:sequence> <xs:sequence>
7
by: rbarschaw | last post by:
I have the following schema designed: <xs:complexType name="AzzFeature-BoxType" mixed="true"> <xs:choice minOccurs="1" maxOccurs="unbounded"> <xs:element ref="Sub-Head" minOccurs="1"...
0
by: =?Utf-8?B?RGFtaWFu?= | last post by:
(This is using .net version 2.0.50727 and system.xml 2.0.50727.42) I am getting an error when serializing using classes generated by xsd.exe. The error is of the type "The Type <typewas not...
0
by: Peter Larsen | last post by:
Is this really a valid schema design? <?xml version="1.0" encoding="UTF-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"> <xs:element name="root">...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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,...
0
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...
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...

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.