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

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><book
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 XmlValidatingReader, 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();
XmlNamespaceManager nsmgr = new XmlNamespaceManager(nt);
nsmgr.AddNamespace(String.Empty, "urn:bookstore-schema");
XmlParserContext ctx = new XmlParserContext(null, nsmgr, null,
XmlSpace.None);
XmlValidatingReader r = new XmlValidatingReader( xmlTxt,
XmlNodeType.Element, ctx);
r.Schemas.Add( schema);
r.ValidationType = ValidationType.Schema;
r.ValidationEventHandler += new ValidationEventHandler (ValidationCallBack);
while (r.Read()){}
The schema was loaded from this:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="urn:bookstore-schema"
elementFormDefault="qualified"
targetNamespace="urn:bookstore-schema">

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

<xsd:complexType name="bookstoreType">
<xsd:sequence maxOccurs="unbounded">
<xsd:element name="book" type="bookType"/>
</xsd:sequence>
</xsd:complexType>

<xsd:complexType name="bookType">
<xsd:sequence>
<xsd:element name="title" type="xsd:string"/>
<xsd:element name="author" type="authorName"/>
<xsd:element name="price" type="xsd:decimal"/>
</xsd:sequence>
<xsd:attribute name="genre" type="xsd:string"/>
</xsd:complexType>

<xsd:complexType name="authorName">
<xsd:sequence>
<xsd:element name="first-name" type="xsd:string"/>
<xsd:element name="last-name" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>

</xsd:schema>
Nov 12 '05 #1
1 2029
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
XMLValidatingReader from this link

http://msdn.microsoft.com/library/default.asp?
url=/library/en-
us/cpguide/html/cpconvalidationofxmlwithxmlvalidatingreader
..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><book
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 XmlValidatingReader, 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();
XmlNamespaceManager nsmgr = new XmlNamespaceManager(nt);
nsmgr.AddNamespace(String.Empty, "urn:bookstore-schema");
XmlParserContext ctx = new XmlParserContext(null, nsmgr, null,XmlSpace.None);
XmlValidatingReader r = new XmlValidatingReader( xmlTxt,
XmlNodeType.Element, ctx);
r.Schemas.Add( schema);
r.ValidationType = ValidationType.Schema;
r.ValidationEventHandler += new ValidationEventHandler (ValidationCallBack);while (r.Read()){}
The schema was loaded from this:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="urn:bookstore-schema"
elementFormDefault="qualified"
targetNamespace="urn:bookstore-schema">

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

<xsd:complexType name="bookstoreType">
<xsd:sequence maxOccurs="unbounded">
<xsd:element name="book" type="bookType"/>
</xsd:sequence>
</xsd:complexType>

<xsd:complexType name="bookType">
<xsd:sequence>
<xsd:element name="title" type="xsd:string"/>
<xsd:element name="author" type="authorName"/>
<xsd:element name="price" type="xsd:decimal"/>

</xsd:sequence>
<xsd:attribute name="genre" type="xsd:string"/>
</xsd:complexType>

<xsd:complexType name="authorName">
<xsd:sequence>
<xsd:element name="first-name" type="xsd:string"/>
<xsd:element name="last-name" type="xsd:string"/>
</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
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...
2
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...
2
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...
2
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....
3
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....
5
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
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...
6
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...
9
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>...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
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...

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.