Groups | Blog | Home
all groups > dotnet xml > may 2006 >

dotnet xml : Determine schemas used by document


Donal McWeeney
5/17/2006 5:21:17 PM
Hi,

Using .Net 2.0 what is the best way to determine the list of schemas used by
a XmlDocument.

Thanks

Donal

Martin Honnen
5/17/2006 6:54:27 PM


[quoted text, click to view]

Each XmlDocument instance has a property named Schemas
<http://msdn2.microsoft.com/en-us/library/system.xml.xmldocument.schemas(VS.80).aspx>
Is that what you are looking for? Note sure what kind of "use" you have
in mind.

--

Martin Honnen --- MVP XML
Donal McWeeney
5/18/2006 12:00:00 AM
Schemas is empty and only used (I think) for the purposes of validation.

I want to do two things:

- dynamically determine what schemas are referenced in a document so I can
build a XmlSchemaSet with the correct file paths to the actual schema files
for validating the document.

- check if a document uses a specific schema and if it does change that
schema name to a different schema...

Thanks

Donal



[quoted text, click to view]

Martin Honnen
5/18/2006 3:22:36 PM


[quoted text, click to view]


So currently you simply load an XML document into an XmlDocument object
but you want to validate while loading using the xsi:schemaLocation (or
xsi:noNamespaceSchemaLocaton) hints in the document?
Then you need to use an XmlReader with the proper settings e.g.

XmlReaderSettings readerSettings = new XmlReaderSettings();
readerSettings.ValidationType = ValidationType.Schema;
readerSettings.ValidationFlags |=
XmlSchemaValidationFlags.ProcessSchemaLocation;
readerSettings.ValidationEventHandler += new
ValidationEventHandler(ValidationHandler);
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(XmlReader.Create(@"file.xml", readerSettings));
// now Schemas is a filled XmlSchemaSet
Console.WriteLine(xmlDocument.Schemas.Count);

I don't think there is any need to look for the schemas yourself, the
framework does that automatically if you use the proper settings as
shown above.


--

Martin Honnen --- MVP XML
Martin Honnen
5/18/2006 3:24:08 PM


[quoted text, click to view]

Does that mean you want to read out the xsi:schemaLocation attribute and
then change its value?

--

Martin Honnen --- MVP XML
Donal McWeeney
5/18/2006 4:03:55 PM
Hi Martin,

thanks for the info...

[quoted text, click to view]

In this case changing the namespace name.

I dont think xsi:schemaLocation will work for me... thats why I have to do a
manual mapping.

AddThis Social Bookmark Button