By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,199 Members | 1,458 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,199 IT Pros & Developers. It's quick & easy.

Problems validating an xml file without specifying the default namespace.

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.