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.
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);
. . .
}