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

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 4152
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Iain | last post by:
I've got a system which takes an XML file, translates it into an update gram and then loads it into my database with SQLXML3 (all in dot net). But it's fragile. And the SQLXML 3 error reporting...
5
by: Adam Child | last post by:
Hi All, I'm trying to validate an xml document. I'm having trouble setting the default namespace of the xml document. If I hard encode the namespace in the xml file then everything works fine....
1
by: Ryan | last post by:
I have a very complex XDR schema that uses namespaces: xmlns="urn:schemas-microsoft-com:xml-data" xmlns:b="urn:schemas-microsoft-com:BizTalkServer" xmlns:d="urn:schemas-microsoft-com:datatypes"...
3
by: Shailendra Batham | last post by:
hi guys I need your suggestions / opinion for doing this the right way. I have a XML and a Schema for the same What I want is when its validated against the schema, it should give custom...
2
by: A | last post by:
Hi all! I would like to ask you guys regarding validating an xml document against a dtd file... I'm not quite familiar manipulating xml and I was assigned to do a program that could document...
2
by: Ian | last post by:
I've inherited some xml that doesn't have a namespace and doesn't have a reference to the schema. The schema doesn't have a targetNamespace attribute. I'm trying to build a test tool, in C#, which...
1
by: Chris Lieb | last post by:
I have an XML Schema file that I know is correct becuase I currently use it in a VB6 program to validate XML documents. Also, if I load an XML file into VS2005 that is not valid against this...
2
by: Mark | last post by:
Hi... I've been trying the .Validate() method on the XmlDocument to validate some xml against a schema, but one thing I noted was that unless the document explicitly declares the schema as a...
7
by: =?Utf-8?B?Q29kZVJhem9y?= | last post by:
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.