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)?
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
精彩评论