473,698 Members | 2,023 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 "unqualifie d" 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(s chemaFile);
XmlTextReader xsdTR = new XmlTextReader(x sdStream);
XmlSchemaCollec tion schemaColl = new XmlSchemaCollec tion();
schemaColl.Add( "http://tempuri.org/finance.xsd", xsdTR);

FileStream xmlStream = File.OpenRead(f ileName);
XmlTextReader xmlTR = new XmlTextReader(x mlStream);
XmlValidatingRe ader xmlReader = new XmlValidatingRe ader(xmlTR);
xmlReader.Valid ationType = ValidationType. Schema;
xmlReader.Schem as.Add(schemaCo ll);
xmlReader.Valid ationEventHandl er += new ValidationEvent Handler(xmlRead er_ValidationEv entHandler);

XmlNamespaceMan ager theNSM = new XmlNamespaceMan ager(xmlTR.Name Table);
theNSM.AddNames pace(String.Emp ty, "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 4173
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 "unqualifie d" 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 "unqualifie d" XML files out
there than there are ones that include a namespace reference.

Unfortunatel y, 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(s chemaFile);
XmlTextReade r xsdTR = new XmlTextReader(x sdStream);
XmlSchemaColle ction schemaColl = new XmlSchemaCollec tion();
schemaColl.Add ("http://tempuri.org/finance.xsd", xsdTR);

FileStream xmlStream = File.OpenRead(f ileName);
XmlTextReade r xmlTR = new XmlTextReader(x mlStream);
XmlValidatingR eader xmlReader = new XmlValidatingRe ader(xmlTR);
xmlReader.Vali dationType = ValidationType. Schema;
xmlReader.Sche mas.Add(schemaC oll);
xmlReader.Vali dationEventHand ler += new ValidationEvent Handler(xmlRead er_ValidationEv entHandler);

XmlNamespaceMa nager theNSM = new XmlNamespaceMan ager(xmlTR.Name Table);
theNSM.AddName space(String.Em pty, "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
2420
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 is not absolutely wonderful. So what I want to do is to validate it before I upload it. When I try and do this I get MILLIONS of errors (well lots) complaining about elements like ROOT which are part of the
5
4073
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. But I can't do this because it breaks our old tools which validate the xml to a dtd (error saying the dtd doens't allow xmlns attribute on the root element). So I'm trying to add the default namespace to the xml file by code. As follows:
1
1831
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" Do I need to build a custom validator in order to validate XML documents against this schema? How does the XML parser know how to validate against the extra namespaces?
3
6057
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 errors saying what happened and where...... is it possible to do that ? for eg. if the Attribute is missing or invalid in the following element <Watch ID="999d999">
2
1274
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 some stuff using the DocBook format. Any articles that you might suggest, I would gladly appreciate it!
2
4491
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 will send the xml to the server and then validate the response against the schema. When I try and do this I get a "The attribute targetNamespace does not match the designated namespace URI" error. Here's a snippet of the validation code: ...
1
2878
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 schema, I get XML errors (blue squigglies) in a few places, which indicates to me that I should get validation errors while validating through code. When I try to validate an invalid document, I never get any validation errors. I have tried many...
2
3039
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 default namespace, Validate() just lets it slide through as a success. Is there a way to Validate() either a) mandating against a particular schema, or at the very least b) supplying a default namespace to be used even when not explicitly declared?
7
4239
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 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:
0
8674
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8603
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9157
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9027
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8895
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8861
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7725
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5860
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3046
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.