473,386 Members | 2,114 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.

Problems validating an xml file without specifying the default namespace.

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:
string DefaultNameSpace = "http://www.mydefaultnamespace.com";
NameTable nt = new NameTable();

XmlNamespaceManager nsmgr = new XmlNamespaceManager(nt);
nsmgr.AddNamespace(string.Empty, DefaultNameSpace);

XmlSchemaCollection myXmlSchemaCollection = new
XmlSchemaCollection(nt);
myXmlSchemaCollection.ValidationEventHandler += new
ValidationEventHandler (this.ValidationEventHandle);

myXmlSchemaCollection.Add(DefaultNameSpace, new
XmlTextReader(lblSchemaLocation.Text));

XmlParserContext context = new XmlParserContext(nt, nsmgr, null,
XmlSpace.None);

myXmlValidatingReader = new
XmlValidatingReader(xmlstringtakenfromelsewhere, XmlNodeType.Document,
context);

myXmlValidatingReader.Schemas.Add(myXmlSchemaColle ction);
myXmlValidatingReader.ValidationType = ValidationType.Schema;
I thought that by using the namespace manager that it would set the
default namespace of the xml document. When I run the above code I get
errors saying that it couldn't find the schema information for the
element defaultnamespace:elementname. No schema found to enforce
validation.
The default namespace is being used because it displays it in the
error message but not when it is trying to resolve the elements to the
schema.

I'm very confused can anyone point me in the right direction I seem to
be going round in circles.

Thanks in advance Adam
Nov 12 '05 #1
5 4029
As a very quick test, try associating the default namespace with some
string...
nsmgr.AddNamespace("myns", DefaultNameSpace);

This is important in XPath selections, so it may do the trick for validation
too.

If not we can look further.

steven

"Adam Child" <ad********@oxon.blackwellpublishing.com> wrote in message
news:93**************************@posting.google.c om...
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:
string DefaultNameSpace = "http://www.mydefaultnamespace.com";
NameTable nt = new NameTable();

XmlNamespaceManager nsmgr = new XmlNamespaceManager(nt);
nsmgr.AddNamespace(string.Empty, DefaultNameSpace);

XmlSchemaCollection myXmlSchemaCollection = new
XmlSchemaCollection(nt);
myXmlSchemaCollection.ValidationEventHandler += new
ValidationEventHandler (this.ValidationEventHandle);

myXmlSchemaCollection.Add(DefaultNameSpace, new
XmlTextReader(lblSchemaLocation.Text));

XmlParserContext context = new XmlParserContext(nt, nsmgr, null,
XmlSpace.None);

myXmlValidatingReader = new
XmlValidatingReader(xmlstringtakenfromelsewhere, XmlNodeType.Document,
context);

myXmlValidatingReader.Schemas.Add(myXmlSchemaColle ction);
myXmlValidatingReader.ValidationType = ValidationType.Schema;
I thought that by using the namespace manager that it would set the
default namespace of the xml document. When I run the above code I get
errors saying that it couldn't find the schema information for the
element defaultnamespace:elementname. No schema found to enforce
validation.
The default namespace is being used because it displays it in the
error message but not when it is trying to resolve the elements to the
schema.

I'm very confused can anyone point me in the right direction I seem to
be going round in circles.

Thanks in advance Adam

Nov 12 '05 #2
Adam Child wrote:
myXmlValidatingReader = new
XmlValidatingReader(xmlstringtakenfromelsewhere, XmlNodeType.Document,
context);


I think the mistake is here. Try instead
myXmlValidatingReader = new
XmlValidatingReader(xmlstringtakenfromelsewhere, XmlNodeType.Element,
context);

--
Oleg Tkachenko [XML MVP, XmlInsider]
http://blog.tkachenko.com
Nov 12 '05 #3
Sorry guys neither of these two methods seemed to fix it.

Not sure how giving the default namespace a name would change it
because the xml wouldn't have this prefix on any of the elements.

Changing the nodetype to element throws up an error about the dtd.
Sorry didn't make it very clear about what the xml string was.

Here is a simplified version of the xml document that i'm trying to
validate:

<?xml version='1.0'?>
<!DOCTYPE content PUBLIC "//BLACKWELL PUBLISHING GROUP//DTD 4.0//EN"
"http://www.mydefaultnamespace.com/conent.dtd" []>
<rootnode>
<element1></element1>
<element2></element2>
</rootnode>

If I change the document to the following then it all works fine. But
when I try to parse it to the dtd then this complains that the
attribute xmlns is not allowed. So this isn't an option.

<?xml version='1.0'?>
<!DOCTYPE content PUBLIC "//BLACKWELL PUBLISHING GROUP//DTD 4.0//EN"
"http://www.mydefaultnamespace.com/conent.dtd" []>
<rootnode xmlns="http://www.mydefaultnamespace.com">
<element1></element1>
<element2></element2>
</rootnode>

Sorry won't include the schema it's very complicated but the general
structure is as follows:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.mydefaultnamespace.com/"
xmlns="http://www.mydefaultnamespace.com/"
elementFormDefault="qualified">
</xsd:schema>

Is there some rule about the XmlValidatingReader not using the default
namespace from the XmlNamespaceManager when validating against the
schema?
"Oleg Tkachenko [MVP]" <oleg@NO!SPAM!PLEASEtkachenko.com> wrote in message news:<#M**************@TK2MSFTNGP12.phx.gbl>...
Adam Child wrote:
myXmlValidatingReader = new
XmlValidatingReader(xmlstringtakenfromelsewhere, XmlNodeType.Document,
context);


I think the mistake is here. Try instead
myXmlValidatingReader = new
XmlValidatingReader(xmlstringtakenfromelsewhere, XmlNodeType.Element,
context);

Nov 12 '05 #4
Adam Child wrote:
Sorry guys neither of these two methods seemed to fix it.
Actually I just tried it and it works. Should be some silly typo somewhere.
Not sure how giving the default namespace a name would change it
because the xml wouldn't have this prefix on any of the elements. foo element in a namespace and foo element in no namespace have nothing
in common except local name.
Sorry won't include the schema it's very complicated but the general
structure is as follows:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.mydefaultnamespace.com/"
xmlns="http://www.mydefaultnamespace.com/"
Aha!!!
string DefaultNameSpace = "http://www.mydefaultnamespace.com";


See the difference? That should be the reason.
--
Oleg Tkachenko [XML MVP, XmlInsider]
http://blog.tkachenko.com
Nov 12 '05 #5
Adam Child wrote:
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.


brw, why don't you make your schema chameleon one (no target namespace)?
It's really weird your schema describes a namespace, while instance
documents are in no namespace.
--
Oleg Tkachenko [XML MVP, XmlInsider]
http://blog.tkachenko.com
Nov 12 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: maddalimurali | last post by:
Hello all, My xml and schema file header are as follows. 1) cisGlobals.xsd. <?xml version="1.0" encoding="UTF-8"?> <xsd:schema targetNamespace="http://www.talgov.com/cisGlobals"...
0
by: Mauro | last post by:
Hi, I need a big help to resolve this problem. I need to put a usercontrol in a datagrid: this control check if the code inserted is present in a archive and if not return a error message. (In...
1
by: Scott Chang | last post by:
Hi all, I loaded the following program 'HelloMCPP' to my MS VC++ .NET (2002) that is installed on my Windows XP Professional PC: ------------------------------------- AssemblyInfo.cpp...
1
by: Wallace | last post by:
Hi all, I have a problem on validating a xml fragment using a single namespace schema which spread across multiple schema files using include in the master schema file. No matter how I change...
4
by: James | last post by:
I have a VB windows forms application that accesses a Microsoft Access database that has been secured using user-level security. The application is being deployed using No-Touch deployment. The...
7
by: Bruce HS | last post by:
I'd like to call my ancestor Validation Function every time any control on a Win Form generates a Validating or Validated event. I'm using VB. I've extended Textbox, for instance, to have its...
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...
5
by: Simon | last post by:
I have problem with namespaces. I have a program that consumes the web service and has for instance names space nsProgram. In this program I have defined several classes that I use for storing and...
3
by: O.B. | last post by:
Below is a program that shows a test for marshaling data from a byte array to a class structure. Unfortunately, there are two annoying problems (bugs?) that I can't seem to get around. The...
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: 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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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,...
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,...

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.