473,790 Members | 2,561 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

XMLSchemaCollec tion Validation with xs:choice xs:any elements

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

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

public void SchemaValidate( XmlDocument xmlDoc, string schemaURI,
string urnNamespace)
{
if (!m_SchemaColle ction.Contains( urnNamespace))
{
//add new schema to collection cache if appropriate
m_SchemaCollect ion.Add(urnName space,schemaURI );
}

//set up validating reader
XmlValidatingRe ader reader = new XmlValidatingRe ader(
xmlDoc.OuterXml ,
XmlNodeType.Doc ument,
null);

reader.Namespac es = true;
reader.Schemas. Add(m_SchemaCol lection);
reader.Validati onType = ValidationType. Schema;

ValidationEvent Handler EvtHandler = new
ValidationEvent Handler(this.Va lidationCallBac k);
reader.Validati onEventHandler += EvtHandler;

while (reader.Read())
{
//iterate entire document,buildi ng up XML Doc of ValidationError s
}
if (m_ValErrorsXml Doc.HasChildNod es)
{
#if DEBUG
m_ValErrorsXmlD oc.Save("C:\\te mp\\debug\\Sche maValidationErr ors.xml");
#endif
throw new Exception("Sche ma Validation Errors:" +
Environment.New Line + schemaURI + Environment.New Line +
m_ValErrorsXmlD oc.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)
{

XmlValidatingRe ader reader = new XmlValidatingRe ader(
XML,
XmlNodeType.Doc ument,
null);

XmlReader schemaReader = new XmlTextReader(R ulesSchema);

XmlSchema schema = XmlSchema.Read( schemaReader, new
ValidationEvent Handler(Validat ionCallBack));

reader.Schemas. Add(schema);

while (reader.Read())
{
//iterate entire document,buildi ng up XML Doc of ValidationError s
}

XmlDocument xml = new XmlDocument();

xml.Load(reader );
//xml.LoadXml(Rul esXML);

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 2373
Caching the XmlSchemaCollec tion and using this for validation should be no
different than adding the schemas directly to the XmlValidatingRe ader except
for an improvement in performance.

Can you send the schema and the xml samples?

Thanks,
Priya

"Matthew" <ma************ ****@hotmail.co m> wrote in message
news:4f******** *************** ***@posting.goo gle.com...
I have implemented the xmlSchemaCollec tion object to cache commonly
used schemas, as follows:

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

//set up validating reader
XmlValidatingRe ader reader = new XmlValidatingRe ader(
xmlDoc.OuterXml ,
XmlNodeType.Doc ument,
null);

reader.Namespac es = true;
reader.Schemas. Add(m_SchemaCol lection);
reader.Validati onType = ValidationType. Schema;

ValidationEvent Handler EvtHandler = new
ValidationEvent Handler(this.Va lidationCallBac k);
reader.Validati onEventHandler += EvtHandler;

while (reader.Read())
{
//iterate entire document,buildi ng up XML Doc of ValidationError s
}
if (m_ValErrorsXml Doc.HasChildNod es)
{
#if DEBUG
m_ValErrorsXmlD oc.Save("C:\\te mp\\debug\\Sche maValidationErr ors.xml");
#endif
throw new Exception("Sche ma Validation Errors:" +
Environment.New Line + schemaURI + Environment.New Line +
m_ValErrorsXmlD oc.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)
{

XmlValidatingRe ader reader = new XmlValidatingRe ader(
XML,
XmlNodeType.Doc ument,
null);

XmlReader schemaReader = new XmlTextReader(R ulesSchema);

XmlSchema schema = XmlSchema.Read( schemaReader, new
ValidationEvent Handler(Validat ionCallBack));

reader.Schemas. Add(schema);

while (reader.Read())
{
//iterate entire document,buildi ng up XML Doc of ValidationError s
}

XmlDocument xml = new XmlDocument();

xml.Load(reader );
//xml.LoadXml(Rul esXML);

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.microsof t.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 Lakshminarayana n [MSFT]" <pr****@online. microsoft.com> wrote in message news:<41******* *@news.microsof t.com>...
Caching the XmlSchemaCollec tion and using this for validation should be no
different than adding the schemas directly to the XmlValidatingRe ader except
for an improvement in performance.

Can you send the schema and the xml samples?

Thanks,
Priya

"Matthew" <ma************ ****@hotmail.co m> wrote in message
news:4f******** *************** ***@posting.goo gle.com...
I have implemented the xmlSchemaCollec tion object to cache commonly
used schemas, as follows:

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

public void SchemaValidate( XmlDocument xmlDoc, string schemaURI,
string urnNamespace)
{
if (!m_SchemaColle ction.Contains( urnNamespace))
{
//add new schema to collection cache if appropriate
m_SchemaCollect ion.Add(urnName space,schemaURI );
}

//set up validating reader
XmlValidatingRe ader reader = new XmlValidatingRe ader(
xmlDoc.OuterXml ,
XmlNodeType.Doc ument,
null);

reader.Namespac es = true;
reader.Schemas. Add(m_SchemaCol lection);
reader.Validati onType = ValidationType. Schema;

ValidationEvent Handler EvtHandler = new
ValidationEvent Handler(this.Va lidationCallBac k);
reader.Validati onEventHandler += EvtHandler;

while (reader.Read())
{
//iterate entire document,buildi ng up XML Doc of ValidationError s
}
if (m_ValErrorsXml Doc.HasChildNod es)
{
#if DEBUG
m_ValErrorsXmlD oc.Save("C:\\te mp\\debug\\Sche maValidationErr ors.xml");
#endif
throw new Exception("Sche ma Validation Errors:" +
Environment.New Line + schemaURI + Environment.New Line +
m_ValErrorsXmlD oc.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)
{

XmlValidatingRe ader reader = new XmlValidatingRe ader(
XML,
XmlNodeType.Doc ument,
null);

XmlReader schemaReader = new XmlTextReader(R ulesSchema);

XmlSchema schema = XmlSchema.Read( schemaReader, new
ValidationEvent Handler(Validat ionCallBack));

reader.Schemas. Add(schema);

while (reader.Read())
{
//iterate entire document,buildi ng up XML Doc of ValidationError s
}

XmlDocument xml = new XmlDocument();

xml.Load(reader );
//xml.LoadXml(Rul esXML);

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
1864
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"/> <xs:element name="HOUSENUMBER" type="xs:int" minOccurs="1"/> <xs:element name="POSTCODE" type="xs:string"/> </xs:sequence> </xs:complexType>
2
3752
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 namespace="http://www.w3.org/1999/xhtml"/> <xs:element ref="message"/> </xs:choice>
1
2914
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 name="DailyAt" type="xs:duration" /> </xs:choice> </xs:complexType>
0
1274
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 tables. This gets me a dataset with the relevant relationships between tables etc. However, I specify in the xsd that an element of a complexType can be "xs:any". i.e "Allow any other elements to appear here, in addition to those specified in...
4
1731
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"/> <xs:sequence> <xs:element ref="el1"/>
2
4354
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
2459
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" maxOccurs="unbounded"/> <xs:element ref="Title-Text" minOccurs="0" maxOccurs="unbounded"/> <xs:element ref="Text" minOccurs="0" maxOccurs="unbounded"/> <xs:element ref="Link-List" minOccurs="0" maxOccurs="unbounded"/> </xs:choice> <xs:attribute...
0
2145
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 expected. Use the XmlInclude or SoapInclude etc" This error is appearing sprodically, with no obvious pattern. When it occurs, it happens every time we run the code, however changes to our code in creating the objects before they are serialized...
0
2842
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"> <xs:complexType> <xs:sequence> <xs:choice> <xs:element name="e1" minOccurs="0" />
0
9666
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
9512
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
10413
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
10145
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,...
1
7530
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
5422
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5551
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4094
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2909
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.