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

Validating an xsd against a different xsd doesn't throw an error!

I wrote a method to validate and xml file against a schema.
If the file does not conform to the schema, it throws an error. It works
fine except for one curious thing.

If I try to validate an XDocument (containing schema xml) against a schema
url, it validates successfully..... Has anyone ever seen this before or know
why it does this.

Here is the code:

public static bool ConformsToSchema ( this XDocument xDocument, string
schemaUrl, out string errorMessage )
{
XmlSchemaSet xmlSchemaSet = new XmlSchemaSet();
bool? documentValidated = null;

try
{
xmlSchemaSet.Add(null, schemaUrl);
xDocument.Validate(xmlSchemaSet, null);
errorMessage = string.Empty;
documentValidated = true;
}
catch (Exception ex)
{
errorMessage = string.Format("Xml validation error occurred.
Document validated unsuccessfully. {0}", ex.Message);
documentValidated = false;
}

return documentValidated ?? false;
}
Many thanks
Oct 22 '08 #1
7 4200
CodeRazor wrote:
If I try to validate an XDocument (containing schema xml) against a schema
url, it validates successfully..... Has anyone ever seen this before or know
why it does this.
Can you show us the schema and the input XML you use? Does the schema
have a targetNamespace? Does the input XML define elements in a
different namespace?

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Oct 22 '08 #2
As you can see from the snippets below, the schemas are very basic. I would
expect that any xml which does not conform to a schema would throw an error,
(including xsd xml). ,... It makes no differnce whether you validate schema 1
against 2 or the other way round. Validation still doesnt throw an error.
Very odd.

Try the schema validation code against the schema xmls below and you will see.
Many thanks.

Schema 1:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="users">
<xs:complexType>
<xs:sequence>
<xs:element name="user" type="xs:string" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Schema 2:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="users">
<xs:complexType>
<xs:sequence>
<xs:element name="user" type="xs:string" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>

Oct 22 '08 #3

sorry, this is schema 2:
Incidentally however, if you validate either of the schemas against
themselves, it also does not trhow an error...

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="status_codes_mapping">
<xs:complexType>
<xs:sequence>
<xs:element name="database" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="status_code" type="xs:string" maxOccurs="unbounded"/>
</xs:sequence>
<xs:attribute name="name" type="databaseName" use="required"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:simpleType name="databaseName">
<xs:restriction base="xs:string">
<xs:enumeration value="a"/>
<xs:enumeration value="b"/>
<xs:enumeration value="c"/>
<xs:enumeration value="d"/>
</xs:restriction>
</xs:simpleType>
</xs:schema>

Oct 22 '08 #4
CodeRazor wrote:
As you can see from the snippets below, the schemas are very basic. I would
expect that any xml which does not conform to a schema would throw an error,
(including xsd xml). ,... It makes no differnce whether you validate schema 1
against 2 or the other way round. Validation still doesnt throw an error.
Very odd.

Try the schema validation code against the schema xmls below and you will see.
Schema validation works on a namespace basis. If you have a root element
in a certain namespace then the validator looks for a schema with that
targetNamespace to find a matching definition. If it does not find a
schema with the targetNamespace then it does lax validation and only
emits a warning. If you use a validating XmlReader then you can set a
validation flag to see such warnings in your validation event handler.
Unfortunately neither XDocument.Validate nor XmlDocument.Validate allow
you to set that validation flag and see such warnings. So you need to
have your code check xmlDocumentInstance.Root.Namespace.NamespaceName
against the targetNamespace(s) of the schema(s) in your XmlSchemaSet.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Oct 22 '08 #5

thanks martin, but what c# namespace do I need so that I can do :
xmlDocumentInstance.Root.Namespace.NamespaceName

I haven't been able to find it
Oct 22 '08 #6
CodeRazor wrote:
thanks martin, but what c# namespace do I need so that I can do :
xmlDocumentInstance.Root.Namespace.NamespaceName
Sorry, there is a slight error in that expression, it should be
xDocumentInstance.Root.Name.NamespaceName.

Here is a sample, it has
using System.Xml.Linq;
using System.Xml.Schema;

XmlSchemaSet schemaSet = new XmlSchemaSet();
schemaSet.Add(null, @"XMLSchema1.xsd");

XDocument doc = XDocument.Load("XMLFile1.xml");

bool matches = false;

string ns = doc.Root.Name.NamespaceName;

foreach (XmlSchema schema in schemaSet.Schemas())
{
if (ns == schema.TargetNamespace)
{
matches = true;
break;
}
}

Console.WriteLine(matches ? "Schema found." : "No schema
found.");

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Oct 22 '08 #7
thank you so much Martin. I looked at the code snippet you supplied and it
really helps me out a lot. I am really grateful!

Thanks again!

CodeRazor
Oct 22 '08 #8

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

Similar topics

2
by: Will | last post by:
I have been having problems validating an XForms document against the XForms schema located at http://www.w3.org/MarkUp/Forms/2002/XForms-Schema.xsd. I have reduced the XForm to its bare bones...
2
by: Cigdem | last post by:
Hello, I am trying to parse the XML files that the user selects(XML files are on anoher OS400 system called "wkdis3"). But i am permenantly getting that error: Directory0: \\wkdis3\ROOT\home...
1
by: davisjoseph | last post by:
Hi All, I'm using Xerces C++ 2.50 DOM version for Validating a valid XML againt the Schema(XSD) available; But it always shows an error in XSD I suppose; This is the error I'm getting using...
30
by: Toni Mcintyre | last post by:
i'm having 2 problems with the http://validator.w3.org 1. if i have: <meta http-equiv="Content-Script-Type" content="text/javascript"> then why do i need <script type=text/javascript>...
4
by: Jon Poploskie | last post by:
Hello, I have an xsd file that I'm trying to use to validate some xml against. I'm hoping this is a simple problem, but I just don't see it. I get the error "The 'role' element is not...
1
by: Craig Beuker | last post by:
Hello, I am experimenting with this XmlValidatingReader and have a question about how it is working (or not working as would be the case) The sample documents and code are included at the end...
2
by: TS | last post by:
i'm wondering if it is preferred practice to throw exception in this circumstance. I have seen it done like that, but i have also read that you should try to never throw an exception in...
6
by: Andy B | last post by:
I need to make sure that a file saved in a particular place is a valid xml file that fits a certain schema. Where would I get started doing this? The original file would have been created and saved...
4
by: agda.karlberg | last post by:
Hello, I need to remove the DTD reference from an xml document, the reason for this is that we want to validate against a schema instead (which we have locally). It takes up to a minute to fetch...
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...
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:
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...

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.