470,563 Members | 2,374 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,563 developers. It's quick & easy.

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 4009
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Will | last post: by
1 post views Thread by davisjoseph | last post: by
30 posts views Thread by Toni Mcintyre | last post: by
4 posts views Thread by Jon Poploskie | last post: by
1 post views Thread by Craig Beuker | last post: by
4 posts views Thread by agda.karlberg | last post: by
1 post views Thread by livre | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.