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

dotnet xml : Xml Validation Against Schema Facets


Matt
8/6/2004 10:13:43 PM
I know how to use the XmlReader to validate XML against a Schema but how do
I take this one step further and get the Facet information for an invalid
Xml element? I have my own validation event handler and I get the overall
message about the problem but I need to report on what exactly the problem
is. Does any know how to do this? Xml-Spy gives you the exact information
but I need to do it programmatically with customers data and report errors.
They want to know the exact error and right now I have to bring it into
Xml-Spy and create a manual report. .Net has got to have something to do
this!

Thanks,

Matt

Martin Honnen
8/7/2004 5:44:39 PM


[quoted text, click to view]

Your validation event handler is passed an argument with a property
Exception of type XmlSchemaException, if the properties you find there
don't give you the information you are looking for then I don't think
..NET exposes them.



--

Martin Honnen
http://JavaScript.FAQTs.com/
Zafar Abbas [MSFT]
8/8/2004 6:52:12 PM
There is a SchemaType property on the XmlValidatingReader which exposes the
XSD schema information of the SOM Element or Attribute currently being
validated in the XML Document. If your element is of a simple type with
certain facets, then while validating that element, you can access the
details of the schema facets this property.

for example: consider the XSD:

<xs:element name="root" type="rootType"/>
<xs:simpleType name="rootType">
<xs:restriction base="xs:string">
<xs:enumeration value="a" />
</xs:restriction>
</xs:simpleType>

the following XML instance is invalid according to its facet in the schema.

<root>b</root>

while reading the document you can access the facets on the element like:

while (vr.Read())
{
if (vr.LocalName == "root" && vr.NodeType==XmlNodeType.Element)
{
XmlSchemaSimpleType st = (XmlSchemaSimpleType)vr.SchemaType;
XmlSchemaSimpleTypeRestriction str =
(XmlSchemaSimpleTypeRestriction)st.Content;
foreach (XmlSchemaObject obj in str.Facets)
{
Console.WriteLine(obj.ToString());
}
}
}


if you are using the new validating XmlReaer (in Visual Studio 2005 beta1)
then the same property can be accessed via
reader.SchemaInfo.SchemaType.

Hope this helps,
Zafar


[quoted text, click to view]

Matt
8/9/2004 11:35:43 AM
Zafar,

Thanks for the information, this is exactly what I am looking for!

Matt

[quoted text, click to view]

AddThis Social Bookmark Button