.Net SvcUtil: Attributes Must Be Optional

I am trying to create C # code classes using SvcUtil.exe instead of Xsd.exe. The latter gives me some problems.

Command line:

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

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

One problem that I can’t solve is: Error: the type “DatafieldDescription” in the namespace “cannot be imported. Attributes must be optional and from the namespace http://schemas.microsoft.com/2003/10/Seri known share / ". Either modify the schema so that types can match data contract types, either 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 need to remove them (in this case, this completion is complete)?

+4
source share
1 answer

The answer and a possible solution can be found here: MSDN: Importing a scheme for generating classes

Specific: XsdDataContractImporter supports a limited subset of the schema. If unsupported design schemas ( such as XML attributes ) are present, the import attempt fails with an exception. However , setting the ImportXmlType property to true expands the range of supported schemas . If set to true, XsdDataContractImporter generates types that implement the IXmlSerializable interface. This provides direct access to the XML representation of these types.

How in:

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

Unfortunately, this will lead to this kind of code:

 private System.Xml.XmlNode[] nodesField; 

Regards, Michelle

+5
source

Source: https://habr.com/ru/post/1311383/


All Articles