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