Namespace exploring with XmlDocument

  • Thread starter Thread starter MaxMax
  • Start date Start date
M

MaxMax

If I have an XmlDocument and I want to discover which namespaces it has, how
should I do? And is the schemaLocation attribute automatically expanded
somewhere? (the schemaLocation has this format:
namespacename filename [namespacename filename] [namespacename filename]
.... )
I want to validate the XML file to its namespaces, but if I can't find which
namespaces it has, how should I do?
(and I need the name of the various namespaces for other reasons).

--- bye
 
The namespaces are nothing more than unique identifiers for the schema
that you expect. Having the namespace itself doesn't do anything. Rather,
you need to have the schema beforehand if you are going to do any sort of
validation.

If you want to get all the namespaces that are in the document, then you
will have to cycle through all the nodes in the document and then get the
namespaces.

You ^might^ be able to get the namespaces after parsing by using the
XmlNameTable instance that the document exposes (it might be in there).
 
Hi Max,

I agree with Nicholas. For XML Schema validation, you should manually add
the schemaset you want to validate the document against before the
validation execute(through the XmlReader's Settings in .net framework 2.0).
e.g.

===================
XmlSchemaSet sc = new XmlSchemaSet();

// Add the schema to the collection.
sc.Add("urn:bookstore-schema", "books.xsd");

// Set the validation settings.
XmlReaderSettings settings = new XmlReaderSettings();
settings.ValidationType = ValidationType.Schema;
settings.Schemas = sc;
settings.ValidationEventHandler += new ValidationEventHandler
(ValidationCallBack);

// Create the XmlReader object.
XmlReader reader = XmlReader.Create("booksSchemaFail.xml", settings);

// Parse the file.
while (reader.Read());
======================

Also, if you need to dynamically locate all the declared xml schemas in a
xml document, you may consider use xmlReader to loop through the XML
document and find all the namespaces declareation in all the nodes before
perform validation. This is may require an additional loop through the
document.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.
 
Hi Max,

Have you got any further ideas on this or does the suggestion in former
messages help you some? If there is anything else we can help, please feel
free to post here.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.
 
MaxMax said:
If I have an XmlDocument and I want to discover which namespaces it has, how
should I do? And is the schemaLocation attribute automatically expanded
somewhere? (the schemaLocation has this format:
namespacename filename [namespacename filename] [namespacename filename]
... )
I want to validate the XML file to its namespaces, but if I can't find which
namespaces it has, how should I do?
(and I need the name of the various namespaces for other reasons).

If you want to validate against the schemas listed in the schemaLocation
attribute then you need to use XmlReaderSettings with the
ValidationFlags set to include ProcessSchemaLocation so like this
XmlReaderSettings readerSettings = new XmlReaderSettings();
readerSettings.ValdiationFlags |=
XmlSchemaValidationFlags.ProcessSchemaLocation;
readerSettings.ValidationEventHandler += delegate (object sender,
ValdiationEventArgs vargs) {
// handle validation errors here
}
using (XmlReader xmlReader = XmlReader.Create(@"file.xml",
readerSettings)) {
while (xmlReader.Read()) {}
}
 
Back
Top