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

dotnet xml : XmlSchemaElement.SchemaType returns null - bug?



eXavier
9/8/2004 4:43:17 PM
Hi all,
I wrote some generator of classes from XSD files but encountred =
unexpected (for me) values in parsed DOM.

First I load XSD with XmlSchema.Read() method, then iterate through =
XmlSchemaElements. I have a function
IsComplex() which return bool value if it has simple content (e.g. type =
string, int,...) or complex one (sequence,
choice, ..). This function looks like this:

bool IsComplex(XmlSchemaElement el) {
if (el.SchemaType is XmlSchemaComplexType) {
XmlSchemaComplexType t =3D (XmlSchemaComplexType)el.SchemaType;
if (t.ContentModel is XmlSchemaSimpleContent)
return false;
else
return true;
}
else
return false;
}

but when I use definition of type within XSD, SchemaType property of =
element returns null. I would expect here
instance of XmlSchemaComplexType. However SchemaTypeName is not null and =
contains correct value=20
(PackageList for example bellow).
Is it correct behaviour ? Is there better approach how to recognize =
content of schema element ?

Here's sample of XSD:

<?xml version=3D"1.0" encoding=3D"utf-16"?>
<xs:schema xmlns=3D"http://test.shipments" =
targetNamespace=3D"http://test.shipments" =
xmlns:xs=3D"http://www.w3.org/2001/XMLSchema">
<xs:element name=3D"shipments">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs=3D"0" maxOccurs=3D"unbounded" =
name=3D"shipment">
<xs:complexType>
<xs:sequence>
<xs:element name=3D"shipment_reference" type=3D"xs:string" =
/>
<xs:element name=3D"packages" type=3D"PackageList" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>

<xs:complexType name=3D"PackageList">
<xs:sequence>
<xs:element name=3D"package_reference" type=3D"xs:string" />
</xs:sequence>
</xs:complexType>

</xs:schema>


Thanks for any help,
Zafar Abbas [MSFT]
9/8/2004 5:19:39 PM
You must compile your schema using XmlSchema.Compile and then access the =
ElementType property to obtain an instance of the XmlSchemaType =
corresponding to the type in the schema.


[quoted text, click to view]
Hi all,
I wrote some generator of classes from XSD files but encountred =
unexpected (for me) values in parsed DOM.

First I load XSD with XmlSchema.Read() method, then iterate through =
XmlSchemaElements. I have a function
IsComplex() which return bool value if it has simple content (e.g. =
type string, int,...) or complex one (sequence,
choice, ..). This function looks like this:

bool IsComplex(XmlSchemaElement el) {
if (el.SchemaType is XmlSchemaComplexType) {
XmlSchemaComplexType t =3D =
(XmlSchemaComplexType)el.SchemaType;
if (t.ContentModel is XmlSchemaSimpleContent)
return false;
else
return true;
}
else
return false;
}

but when I use definition of type within XSD, SchemaType property of =
element returns null. I would expect here
instance of XmlSchemaComplexType. However SchemaTypeName is not null =
and contains correct value=20
(PackageList for example bellow).
Is it correct behaviour ? Is there better approach how to recognize =
content of schema element ?

Here's sample of XSD:

<?xml version=3D"1.0" encoding=3D"utf-16"?>
<xs:schema xmlns=3D"http://test.shipments" =
targetNamespace=3D"http://test.shipments" =
xmlns:xs=3D"http://www.w3.org/2001/XMLSchema">
<xs:element name=3D"shipments">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs=3D"0" maxOccurs=3D"unbounded" =
name=3D"shipment">
<xs:complexType>
<xs:sequence>
<xs:element name=3D"shipment_reference" =
type=3D"xs:string" />
<xs:element name=3D"packages" type=3D"PackageList" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>

<xs:complexType name=3D"PackageList">
<xs:sequence>
<xs:element name=3D"package_reference" type=3D"xs:string" />
</xs:sequence>
</xs:complexType>

</xs:schema>


Thanks for any help,
eXavier
9/9/2004 8:38:31 AM
Thanks a lot, it works as I need.
However, MSDN class library is poor documentation for using SOM, is =
there some good source of information on working with SOM ?
I would rather avoid this try-fail approach next time.

eXavier

[quoted text, click to view]
You must compile your schema using XmlSchema.Compile and then access =
the ElementType property to obtain an instance of the XmlSchemaType =
corresponding to the type in the schema.


[quoted text, click to view]
Hi all,
I wrote some generator of classes from XSD files but encountred =
unexpected (for me) values in parsed DOM.

First I load XSD with XmlSchema.Read() method, then iterate through =
XmlSchemaElements. I have a function
IsComplex() which return bool value if it has simple content (e.g. =
type string, int,...) or complex one (sequence,
choice, ..). This function looks like this:

bool IsComplex(XmlSchemaElement el) {
if (el.SchemaType is XmlSchemaComplexType) {
XmlSchemaComplexType t =3D =
(XmlSchemaComplexType)el.SchemaType;
if (t.ContentModel is XmlSchemaSimpleContent)
return false;
else
return true;
}
else
return false;
}

but when I use definition of type within XSD, SchemaType property of =
element returns null. I would expect here
instance of XmlSchemaComplexType. However SchemaTypeName is not null =
and contains correct value=20
(PackageList for example bellow).
Is it correct behaviour ? Is there better approach how to recognize =
content of schema element ?

Here's sample of XSD:

<?xml version=3D"1.0" encoding=3D"utf-16"?>
<xs:schema xmlns=3D"http://test.shipments" =
targetNamespace=3D"http://test.shipments" =
xmlns:xs=3D"http://www.w3.org/2001/XMLSchema">
<xs:element name=3D"shipments">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs=3D"0" maxOccurs=3D"unbounded" =
name=3D"shipment">
<xs:complexType>
<xs:sequence>
<xs:element name=3D"shipment_reference" =
type=3D"xs:string" />
<xs:element name=3D"packages" type=3D"PackageList" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>

<xs:complexType name=3D"PackageList">
<xs:sequence>
<xs:element name=3D"package_reference" type=3D"xs:string" />
</xs:sequence>
</xs:complexType>

</xs:schema>


Thanks for any help,
AddThis Social Bookmark Button