Groups | Blog | Home
all groups > dotnet xml > august 2004 >

dotnet xml : Get XmlSchemaElement from Instance Element


josh NO[at]SPAM nautilusnet.com
8/5/2004 7:00:46 PM
Is there anyway to get the correct XmlSchemaElement from an XSD that
corresponds to an element within an Xml Document Instance?

I want to automatically generate a UI for an Xml Instance based upon
information contained within an XSD. Based on the selected element
within the Xml Instance document I want to find the corresponding
XmlSchemaElement in the XSD and then get parameters from the XSD to
customize the UI (min, max, allowable children, etc...).


Please help, I am driving myself nuts trying to figure out how to do
this. I thought I might be able to do it with the XmlValidatingReader
but it doesn't look like it will give me the XmlSchemaElement, just
the type....


Zafar Abbas [MSFT]
8/5/2004 10:44:15 PM
You are not able to access the SOM Element (XmlSchemaElement) from the
XmlValidatingReader. The new validating reader shipped in System.Xml in
Visual Studio 2005 beta1 does expose the SOM Element. You will have to
modify your code to work with this new validating reader.
In Visual Studio 2005 beta1, use the XmlSchemaSet class which is a
replacement for the XmlSchemaCollection. Below is a code sample to create
the reader and using it. At any point during validation, you can access the
SOM objects(XmlSchemaElement and XmlSchemaAttribute) from

vr.SchemaInfo.XmlSchemaElement
vr.SchemaInfo.XmlSchemaAttribute

Hope this helps,

-Zafar

Code Sample:
XmlReaderSettings settings = new XmlReaderSettings();
settings.XsdValidate = true;
settings.Schemas.ValidationEventHandler += new
ValidationEventHandler(ValidationCallback);
settings.ValidationEventHandler += new
ValidationEventHandler(ValidationCallback);
settings.Schemas.Add(null, "SCHEMA.XSD");
XmlReader vr = XmlReader.Create(xmlFile, null, new XmlUrlResolver(),
settings);
while(vr.Read()) {}







[quoted text, click to view]

AddThis Social Bookmark Button