Generate DataContract with SVCUTIL
I'm trying to generate a data contract from an XSD. I run "svcutil /dconly c:\projects\schema.xsd /language:CS /out:c:\projects\class.cs" and I get the following error: cannot read <schema file name>. cannot load file <schema name> as an assembly. check the fusion logs for more information. could not load file or assembly 'file...' or one or more of its dependancies. The module was expected to contain an assembly manifest"
This is error is confusing to me because I cannot see anything that will make svcutil think this should be an assembly. It runs fine with the same command pointing to another xsd; I only get this error with this particular xsd. Any ideas? xsd pasted below...
<?xml version="1.0" encoding="utf-16"?>
<xs:schema attributeFormDefault="qualified" elementFormDefault="qualified"
xmlns:b="http://schemas.microsoft.com/BizTalk/2003"
xmlns="http://CGX.StoreFront.BizTalk.Schemas.ShoppingCart"
targetNamespace="http://CGX.StoreFront.BizTalk.Schemas.ShoppingCart" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:simpleType name="stringType">
<xs:restriction base="xs:string"/>
</xs:simpleType>
<xs:simpleType name="intType">
<xs:restriction base="xs:positiveInteger"/>
</xs:simpleType>
<xs:simpleType name="floatType">
<xs:restriction base="xs:float"/>
</xs:simpleType>
<xs:simpleType name="orderNoType">
<xs:restriction base="xs:string">
<xs:pattern value="[0-9]{3}[rR][0-9]{3}"/>
</xs:restriction>
</xs:simpleType>
<xs:complexType name="orderItemType">
<xs:sequence>
<xs:element name="product" type="stringType"/>
<xs:element name="quantity" type="intType"/>
<xs:element name="price" type="floatType"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="orderType">
<xs:sequence>
<xs:attribute name="orderNo" type="orderNoType" use="required"/>
<xs:attribute name=”checkoutDate” type=”xs:date” use=”required”/>
<xs:element name="orderItem" maxOccurs="unbounded" type="orderItemType"/>
<xs:attribute name="tax" type="xs:floatType" use="required"/>
<xs:attribute name="total" type="xs:floatType" use="required"/>
</xs:sequence>
</xs:complexType>
<xs:element name="order" type="orderType"/>
</xs:schema>
thanks,
Doug
[3420 byte] By [
DouglasR] at [2007-12-30]
it seems svcutil has some problem to determine schema.xsd is a xsd file and then try to read it as an assembly, I changed the xml tag of it to
<?xml version="1.0" encoding="utf-8" ?>
now it throw this error
Error: Cannot read temp.xsd.
XML Schema Parsing Error while reading: 'temp.xsd' verify that the XML is both well-formed and valid.
The 'http://www.w3.org/2001/XMLSchema:attribute' element is not supported in this context.
If you would like more help, type "svcutil /?"
Unfortnately there is no line# for the error but I think
<xs:complexType name="orderType">
<xs:sequence>
<xs:attribute name="orderNo" type="orderNoType" use="required"/>
<xs:attribute name=”checkoutDate” type=”xs:date” use=”required”/>
<xs:element name="orderItem" maxOccurs="unbounded" type="orderItemType"/>
<xs:attribute name="tax" type="xs:floatType" use="required"/>
<xs:attribute name="total" type="xs:floatType" use="required"/>
</xs:sequence>
</xs:complexType>
has a problem, xs:attribute can't be inside the sequence
Thank you for the response Joe. I changed the attributes to elements, and made some other minor changes related to that and I'm getting a different error now. The new xsd is below.
<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="qualified" elementFormDefault="qualified"
xmlns:b="http://schemas.microsoft.com/BizTalk/2003"
xmlns="http://CGX.StoreFront.BizTalk.Schemas.ShoppingCart"
targetNamespace="http://CGX.StoreFront.BizTalk.Schemas.ShoppingCart" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:simpleType name="stringType">
<xs:restriction base="xs:string"/>
</xs:simpleType>
<xs:simpleType name="intType">
<xs:restriction base="xs:positiveInteger"/>
</xs:simpleType>
<xs:simpleType name="floatType">
<xs:restriction base="xs:float"/>
</xs:simpleType>
<xs:simpleType name="orderNoType">
<xs:restriction base="xs:string">
<xs:pattern value="[0-9]{3}[rR][0-9]{3}"/>
</xs:restriction>
</xs:simpleType>
<xs:complexType name="orderItemType">
<xs:sequence>
<xs:element name="product" type="stringType"/>
<xs:element name="quantity" type="intType"/>
<xs:element name="price" type="floatType"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="orderType">
<xs:sequence>
<xs:element name="orderNo" type="orderNoType" minOccurs="1" maxOccurs="1"/>
<xs:element name="checkoutDate" type="xs:date" minOccurs="1" maxOccurs="1"/>
<xs:element name="orderItem" maxOccurs="unbounded" minOccurs="1" type="orderItemType"/>
<xs:element name="tax" type="xs:floatType" minOccurs="1" maxOccurs="1"/>
<xs:element name="total" type="xs:floatType" minOccurs="1" maxOccurs="1"/>
</xs:sequence>
</xs:complexType>
<xs:element name="order" type="orderType"/>
</xs:schema>
The error is telling me that the element "orderItem" must have a maxOccurrs of "1". I've gotten this error with a couple of other xsds I've tried to use. I looked at some information on MSDN that I had a hard time understanding regarding when svcutil will allow you to use maxOccurs and when it will not. We need to be able to have multiple orderItems in an order. We've been able to generate classes using xsd.exe with similar xsds, but svcutil always gives us an error. We'd like to use svcutil to generate the Data Contracts to streamline the development of our WCF services and not have to use the XmlSerializerFormatAttribute. Is there something we doing fundamentally wrong with our xsds that is causing svcutil to not like the maxOccurs?
Thanks for the help.
Doug
If the schema of your message have to confirm to some schema, then yes, you'll have to use xsd.exe generated classes and make your service with [XmlSerializerFormat].
Another option is still use let svcutil generate datacontract classes, in which case it'll generate IXmlserializable and you'll have to fill in and do the readxml/writexml yourself.