473,218 Members | 1,536 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,218 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 4185
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: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.