469,903 Members | 1,651 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

Validating XML w/o Namespace against XSD

I am trying to validate an XML file that does not contain any xmlns: namespace attribute against a
predefined XSD schema.

I should think this would be easy to do, since there must be far more "unqualified" XML files out
there than there are ones that include a namespace reference.

Unfortunately, as I am coming to hate about all things XML, the easy things are extraordinarily
difficult and/or subtle.

I know how to create a schema collection dynamically and validate qualified XML files against it.
But I can't figure out the "no namespace in the XML file" situation (other than rewriting the XML
file to include an xmlns: attribute at the root, but that's too ugly to think about).

I tried, among many other things:

FileStream xsdStream = File.OpenRead(schemaFile);
XmlTextReader xsdTR = new XmlTextReader(xsdStream);
XmlSchemaCollection schemaColl = new XmlSchemaCollection();
schemaColl.Add("http://tempuri.org/finance.xsd", xsdTR);

FileStream xmlStream = File.OpenRead(fileName);
XmlTextReader xmlTR = new XmlTextReader(xmlStream);
XmlValidatingReader xmlReader = new XmlValidatingReader(xmlTR);
xmlReader.ValidationType = ValidationType.Schema;
xmlReader.Schemas.Add(schemaColl);
xmlReader.ValidationEventHandler += new ValidationEventHandler(xmlReader_ValidationEventHa ndler);

XmlNamespaceManager theNSM = new XmlNamespaceManager(xmlTR.NameTable);
theNSM.AddNamespace(String.Empty, "http://tempuri.org/finance.xsd");

but that doesn't work, blowing up on the root element of the XML file, complaining that it can't
find schema information for the unqualified root element name.

So how the heck do I validate an XML file that has no namespace attributes???

- Mark
Nov 12 '05 #1
1 3902
Okay, I win the bozo award today.

It turns out three is a simple answer, but it's not in the code; it's in the XSD file. Stripping out
all the specific namespace references in the XSD file, and then using String.Empty as the target
namespace in the code, lets me validate "unqualified" XML files.

- Mark
On Tue, 09 Dec 2003 21:25:47 -0800, Mark Olbert <ma**@arcabama.com> wrote:
I am trying to validate an XML file that does not contain any xmlns: namespace attribute against a
predefined XSD schema.

I should think this would be easy to do, since there must be far more "unqualified" XML files out
there than there are ones that include a namespace reference.

Unfortunately, as I am coming to hate about all things XML, the easy things are extraordinarily
difficult and/or subtle.

I know how to create a schema collection dynamically and validate qualified XML files against it.
But I can't figure out the "no namespace in the XML file" situation (other than rewriting the XML
file to include an xmlns: attribute at the root, but that's too ugly to think about).

I tried, among many other things:

FileStream xsdStream = File.OpenRead(schemaFile);
XmlTextReader xsdTR = new XmlTextReader(xsdStream);
XmlSchemaCollection schemaColl = new XmlSchemaCollection();
schemaColl.Add("http://tempuri.org/finance.xsd", xsdTR);

FileStream xmlStream = File.OpenRead(fileName);
XmlTextReader xmlTR = new XmlTextReader(xmlStream);
XmlValidatingReader xmlReader = new XmlValidatingReader(xmlTR);
xmlReader.ValidationType = ValidationType.Schema;
xmlReader.Schemas.Add(schemaColl);
xmlReader.ValidationEventHandler += new ValidationEventHandler(xmlReader_ValidationEventHa ndler);

XmlNamespaceManager theNSM = new XmlNamespaceManager(xmlTR.NameTable);
theNSM.AddNamespace(String.Empty, "http://tempuri.org/finance.xsd");

but that doesn't work, blowing up on the root element of the XML file, complaining that it can't
find schema information for the unqualified root element name.

So how the heck do I validate an XML file that has no namespace attributes???

- Mark


Nov 12 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Iain | last post: by
3 posts views Thread by Shailendra Batham | last post: by
7 posts views Thread by =?Utf-8?B?Q29kZVJhem9y?= | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.