473,395 Members | 1,440 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,395 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 4030
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: 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...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
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.