1

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.

Tom Tom
  • 423
  • 4
  • 9

1 Answers1

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); 
  . . . 
}
MiMo
  • 11,793
  • 1
  • 33
  • 48
  • Thank you for your insight! I can't upvote on this account, but I would 100x! I sank a whole day into this... Another wonderful case of learning the hard way. This raises a number of other questions (topic for conversation elsewhere) but the most prominent seems relevant: is an XSD using the original declaration (root element, not complexType) considered invalid? I ask because A) XmlSpy reported a valid schema and B) I copied the format from a published schema I'm working with to replicate the error - it makes the same 'mistake'. – Tom Tom Feb 21 '13 at 20:03
  • @TomTom: you're welcome. Your original schema was valid - the problem is that the `XmlSchemaProvider` machinery expects either a name of a complex type or directly a `XmlSchemaComplexType` object, and you were providing instead the name of an element definition. – MiMo Feb 21 '13 at 20:42