Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.Net SvcUtil: attributes must be optional

I'm trying to generate C# code classes with SvcUtil.exe instead of Xsd.exe. The latter is giving me some problems.

Command line:

SvcUtil.exe myschema.xsd /dconly /ser:XmlSerializer

Several SvcUtil problems are described and solved here: http://blog.shutupandcode.net/?p=761

One problem I can't solve is this one: Error: Type 'DatafieldDescription' in namespace '' cannot be imported. Attributes must be optional and from namespace 'http://schemas.microsoft.com/2003/10/Seri alization/'. Either change the schema so that the types can map to data contract types or use ImportXmlType or use a different serializer. '

I changed

<xs:attribute name="Order" use="required">

to

<xs:attribute name="Order" use="optional">

and

<xs:attribute name="Order">

But the error remains. Is it possible to use attributes, or do I have to delete them all (in that case, this excercition is over)?

like image 580
Michel van Engelen Avatar asked May 31 '10 07:05

Michel van Engelen


1 Answers

The answer, and a possible solution, can be found here: MSDN: Importing Schema to Generate Classes

Specific: The XsdDataContractImporter supports a limited subset of the schema. If unsupported schema constructs are present (for example, XML attributes), the import attempt fails with an exception. However, setting the ImportXmlType property to true extends the range of schema supported. When set to true, the XsdDataContractImporter generates types that implement the IXmlSerializable interface. This enables direct access to the XML representation of these types.

As in:

SvcUtil.exe myschema.xsd /dconly /ser:XmlSerializer /importXmlTypes

Sadly enough, this will result in this kind of code:

private System.Xml.XmlNode[] nodesField;

Regards, Michel

like image 182
Michel van Engelen Avatar answered Sep 28 '22 04:09

Michel van Engelen