473,796 Members | 2,618 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

xml validation against schema

Hi,
I need to validate XML fragment against XSD schema. The main issue is that
xml fragment does not contain refrence to schema, but I want to force the
validation against the schema I have in XmlSchema object.

The scenario is as follows:
I have

string fragment;

containing xml fragment, e.g. something like
"<bookstore><bo ok
genre="novel">< title>Abc</title><author>< first-name>A</first-name><last-name
B</last-name></author><price>8 .99</price></book></bookstore>"

but this xml is just fragment of larger xml document received from third
party;
and

XmlSchema schema;

containing loaded XSD of bookstore.

I need code snippet to perform validation of fragment against schema.

Thanks,
eXavier


I played a little with XmlValidatingRe ader, but I am no guru in XSDs and
..NET System.XML namespace, so I don't much understand what namespaces to set

NameTable nt = new NameTable();
XmlNamespaceMan ager nsmgr = new XmlNamespaceMan ager(nt);
nsmgr.AddNamesp ace(String.Empt y, "urn:bookst ore-schema");
XmlParserContex t ctx = new XmlParserContex t(null, nsmgr, null,
XmlSpace.None);
XmlValidatingRe ader r = new XmlValidatingRe ader( xmlTxt,
XmlNodeType.Ele ment, ctx);
r.Schemas.Add( schema);
r.ValidationTyp e = ValidationType. Schema;
r.ValidationEve ntHandler += new ValidationEvent Handler (ValidationCall Back);
while (r.Read()){}
The schema was loaded from this:

<xsd:schema xmlns:xsd="http ://www.w3.org/2001/XMLSchema"
xmlns="urn:book store-schema"
elementFormDefa ult="qualified"
targetNamespace ="urn:bookst ore-schema">

<xsd:element name="bookstore " type="bookstore Type"/>

<xsd:complexTyp e name="bookstore Type">
<xsd:sequence maxOccurs="unbo unded">
<xsd:element name="book" type="bookType"/>
</xsd:sequence>
</xsd:complexType >

<xsd:complexTyp e name="bookType" >
<xsd:sequence >
<xsd:element name="title" type="xsd:strin g"/>
<xsd:element name="author" type="authorNam e"/>
<xsd:element name="price" type="xsd:decim al"/>
</xsd:sequence>
<xsd:attribut e name="genre" type="xsd:strin g"/>
</xsd:complexType >

<xsd:complexTyp e name="authorNam e">
<xsd:sequence >
<xsd:element name="first-name" type="xsd:strin g"/>
<xsd:element name="last-name" type="xsd:strin g"/>
</xsd:sequence>
</xsd:complexType >

</xsd:schema>
Nov 12 '05 #1
1 2063
I am not sure if one can validate a fragment against its
superset's schema, it basically turns the purpose of
schema upside down. You can for sure, try to validate the
XML before it fragments. You can definitely use
XMLValidatingRe ader from this link

http://msdn.microsoft.com/library/default.asp?
url=/library/en-
us/cpguide/html/cpconvalidation ofxmlwithxmlval idatingreader
..asp

useful snippets!





-----Original Message-----
Hi,
I need to validate XML fragment against XSD schema. The main issue is thatxml fragment does not contain refrence to schema, but I want to force thevalidation against the schema I have in XmlSchema object.

The scenario is as follows:
I have

string fragment;

containing xml fragment, e.g. something like
"<bookstore><b ook
genre="novel"> <title>Abc</title><author>< first- name>A</first-name><last-name
B</last-

name></author><price>8 .99</price></book></bookstore>"but this xml is just fragment of larger xml document received from thirdparty;
and

XmlSchema schema;

containing loaded XSD of bookstore.

I need code snippet to perform validation of fragment against schema.
Thanks,
eXavier


I played a little with XmlValidatingRe ader, but I am no guru in XSDs and..NET System.XML namespace, so I don't much understand what namespaces to set
NameTable nt = new NameTable();
XmlNamespaceMa nager nsmgr = new XmlNamespaceMan ager(nt);
nsmgr.AddNames pace(String.Emp ty, "urn:bookst ore-schema");
XmlParserConte xt ctx = new XmlParserContex t(null, nsmgr, null,XmlSpace.None) ;
XmlValidatingR eader r = new XmlValidatingRe ader( xmlTxt,
XmlNodeType.El ement, ctx);
r.Schemas.Ad d( schema);
r.ValidationTy pe = ValidationType. Schema;
r.ValidationEv entHandler += new ValidationEvent Handler (ValidationCall Back);while (r.Read()){}
The schema was loaded from this:

<xsd:schema xmlns:xsd="http ://www.w3.org/2001/XMLSchema"
xmlns="urn:book store-schema"
elementFormDefa ult="qualified"
targetNamespace ="urn:bookst ore-schema">

<xsd:element name="bookstore " type="bookstore Type"/>

<xsd:complexTyp e name="bookstore Type">
<xsd:sequence maxOccurs="unbo unded">
<xsd:element name="book" type="bookType"/>
</xsd:sequence>
</xsd:complexType >

<xsd:complexTyp e name="bookType" >
<xsd:sequence >
<xsd:element name="title" type="xsd:strin g"/>
<xsd:element name="author" type="authorNam e"/>
<xsd:element name="price" type="xsd:decim al"/>

</xsd:sequence>
<xsd:attribut e name="genre" type="xsd:strin g"/>
</xsd:complexType >

<xsd:complexTyp e name="authorNam e">
<xsd:sequence >
<xsd:element name="first-name" type="xsd:strin g"/>
<xsd:element name="last-name" type="xsd:strin g"/>
</xsd:sequence>
</xsd:complexType >

</xsd:schema>
.

Nov 12 '05 #2

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

Similar topics

2
4661
by: Olaf Meyer | last post by:
Apprentently xerces 2.6.0 (Java) does not validate against contraints specified in the schema (e.g. constraints specified via unique element). The validation works with the XML editor I'm using (XMLSpy4) but not with Xerces 2.6.0. I've included a really short and simple example to illustrate it. I would like to get some comments on the validation capabilities of Xerces 2.6.0. I though it *fully* supported W3C Schema!
2
1584
by: John Viele | last post by:
To you XML validation experts: I have a schema that validates an XML file, but there are a couple of other types of validation I'd like to do on it. They are basically string fields that must be validated against a set of possible codes, but there could be tens of thousands of possible codes. Naturally, these codes are in a table in SQL (actually there are a few code types, but the problem is the same for each). In the past I have...
2
5188
by: Sudip Chakraborty | last post by:
Is there a way to see constraint validation errors while loading xml into a DataSet ? I'm interested in the line number in the xml file which is causing the error. I've enclosed the relevant stack trace below. at System.Data.DataSet.FailedEnableConstraints() at System.Data.DataSet.EnableConstraints() at System.Data.DataSet.set_EnforceConstraints(Boolean value)
2
9453
by: Shone | last post by:
I would like to perform a 2-pass XML reading from a stream. Once using the Validating reader, just to confirm the validity against the schema, and next time to do a reading to extract the data. Actually, second time I do a deserialization, the data from XML is fed directly to an object. The problem I am experiencing is the error at the second reading attempt, and error description implies that reader is winded to the end of the XML and...
3
7421
by: Dave | last post by:
Hi, I'm really confused as to how to validate XML fragments against a schema in C#. I am creating XML through an automated process and have an .xsd which was given to me to validate against. Can someone give me some clue as to how to validate my XML string against this schema document? Is there some great secret that I'm missing?
5
4954
by: paul_zaoldyeck | last post by:
does anyone know how to validate an xml file against multiple defined schema? can you show me some examples? i'm making here an xml reader.. thank you
7
10291
by: christian.eickhoff | last post by:
Hi Everyone, I am currently implementing an XercesDOMParser to parse an XML file and to validate this file against its XSD Schema file which are both located on my local HD drive. For this purpose I set the corresponding XercesDOMParser feature as shown in the upcoming subsection of my code. As far as I understand, the parsing process should throw an DOMException in case the XML file doesn't match the Schema file (e.g. Element...
6
2736
by: lists | last post by:
Hi all, I am trying to validate an XML file against an XSD schema file within a ..NET C++ program, but the validation doesn't seem to be occuring. My code is listed below. The validation event handler is not called even when the XML should valid properly against the schema. Obviously I'm missing something. Can anyone help?
9
3021
by: mstilli | last post by:
Hi, I am trying to use schema for server side validation using xerces to catch the validation errors. validating this XML: <Content4> <textarea13></textarea13> <binaryobject3></binaryobject3>
0
9685
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
10459
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
10187
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
9055
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
7553
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
5446
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
5578
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4120
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
2
3735
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.