Serializing IXmlSerializable Types Using XmlSerializer in WCF

纵然是瞬间 提交于 2019-12-11 04:38:33

问题


Thoroughly frustrated with the DataContractSerializer, I'm trying to get up and running in WCF using IXmlSerializable types with XmlSerializer.

I've implemented IXmlSerializable and XmlSchemaProvider in my class to be serialized and declared [XmlSerializerFormat] for my OperationContract.

Using a complex schema, I get the following error on trying to view the WSDL:

"Schema type information provided by IXmlSerializable is invalid: 
Reference to undeclared attribute group 'anAttributeGroupInMySchema'"

The schema has various includes (this attribute is declared in one of them). I even added the included schemas in code (schema.Includes) but to no avail.

Even in the most trivial example project (simple schema with 1 element and 2 attributes, simple corresponding class, you name it) I finally get past this error, but bump right into:

"WCF_IXmlSerializable.TestClass.TestSchemaProvider() must return a valid type 
name. Type 'TEST' cannot be found in the targetNamespace='www.test.com'."

Sadly I don't know what is a valid type name. It's certainly not an element name from my XSD, AFAICS.

Any ideas?

EDIT:

Example source code can be viewed online here.


回答1:


I see two problems: your test schema does not define a type named TEST_CLASS, it defines an element with that name. The XSD should be something like this:

<xs:schema xmlns="www.test.com" xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" targetNamespace="www.test.com">
  <xs:complexType name="TEST_CLASS">
    <xs:sequence>
      <xs:element name="TEST">
        <xs:complexType>
          <xs:attribute name="TYPE"/>
          <xs:attribute name="DURATION"/>
        </xs:complexType>
      </xs:element>
    </xs:sequence>
  </xs:complexType>
</xs:schema>

The second problem is that XmlSchema objects should be loaded using theXmlSchema.Read() method:

using (XmlReader reader = XmlReader.Create(xsdDir + xsdFile)) {
  XmlSchema schema = XmlSchema.Read(reader, null); 
  . . . 
}


来源:https://stackoverflow.com/questions/14989774/serializing-ixmlserializable-types-using-xmlserializer-in-wcf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!