Search code examples
c#wcfserializationxsdixmlserializable

WCF service xsd after IXmlSerializable implementation is not correct


I have a wcf service (using xmlserialization). There is some class which looks in SoapUI like this:

     <MyClass>
        <propertyA>?</propertyA>
        <propertyB>?</propertyB>
     </MyClass>

I had to implement IXmlSerializable interface on it. After doing it, class has strange structure in SoapUI:

     <MyClass>
        <xs:schema>
           <!--Ignoring type [{http://www.w3.org/2001/XMLSchema}schema]-->
        </xs:schema>
        <!--You may enter ANY elements at this point-->
     </MyClass>

May it be a result of the following implementation of the GetSchema method?

    public XmlSchema GetSchema()
    {
        return null;
    }

Below is section about MyClass from service wsdl:

<xs:element name="MyClass" form="unqualified" maxOccurs="1" minOccurs="0">
  <xs:complexType>
    <xs:sequence>
      <xs:element ref="xs:schema"/>
      <xs:any/>
    </xs:sequence>
  </xs:complexType>
</xs:element>

Solution

  • GetSchema() should always return null. See Proper way to implement IXmlSerializable?.

    Instead, you need to add [XmlSchemaProvider(string methodName)] to your class and implement a static method that returns an XML schema and an XmlQualifiedName (or XmlSchemaType for anonymous types) that specifies the schema of the type.

    For instance, if your original type looks like:

    [DataContract(Namespace = "http://schemas.datacontract.org/2004/07/Question38741035")]
    [XmlRoot(Namespace = "http://schemas.datacontract.org/2004/07/Question38741035")]
    public class MyClass
    {
        [DataMember]
        public string PropertyA { get; set; }
    
        [DataMember]
        public decimal PropertyB { get; set; }
    }
    

    Then your IXmlSerializable re-implementation of the type should look something like:

    [XmlSchemaProvider("GetSchemaMethod")]
    [XmlRoot(Namespace = "http://schemas.datacontract.org/2004/07/Question38741035")]
    public class MyClass : IXmlSerializable
    {
        public string PropertyA { get; set; }
    
        public decimal PropertyB { get; set; }
    
        const string XmlNamespace = "http://schemas.datacontract.org/2004/07/Question38741035";
    
        // This is the method named by the XmlSchemaProviderAttribute applied to the type.
        public static XmlQualifiedName GetSchemaMethod(XmlSchemaSet xs)
        {
            string schema = @"<?xml version=""1.0"" encoding=""utf-16""?>
    <xs:schema 
        xmlns:tns=""http://schemas.datacontract.org/2004/07/Question38741035"" 
        elementFormDefault=""qualified"" 
        targetNamespace=""http://schemas.datacontract.org/2004/07/Question38741035"" 
        xmlns:xs=""http://www.w3.org/2001/XMLSchema"">
      <xs:complexType name=""MyClass"">
        <xs:sequence>
          <xs:element minOccurs=""0"" name=""PropertyA"" nillable=""true"" type=""xs:string"" />
          <xs:element minOccurs=""0"" name=""PropertyB"" type=""xs:decimal"" />
        </xs:sequence>
      </xs:complexType>
      <xs:element name=""MyClass"" nillable=""true"" type=""tns:MyClass"" />
    </xs:schema>";
    
            using (var textReader = new StringReader(schema))
            using (var schemaSetReader = System.Xml.XmlReader.Create(textReader))
            {
                xs.Add(XmlNamespace, schemaSetReader);
            }
            return new XmlQualifiedName("MyClass", XmlNamespace);
        }
    
        #region IXmlSerializable Members
    
        public System.Xml.Schema.XmlSchema GetSchema()
        {
            return null;
        }
    
        public void ReadXml(System.Xml.XmlReader reader)
        {
            if (reader.IsEmptyElement)
            {
                reader.Read();
                return;
            }
    
            var node = (XElement)XNode.ReadFrom(reader);
            if (node != null)
            {
                var ns = (XNamespace)XmlNamespace;
    
                PropertyA = (string)node.Element(ns + "PropertyA");
                PropertyB = (decimal)node.Element(ns + "PropertyB");
            }
        }
    
        public void WriteXml(System.Xml.XmlWriter writer)
        {
            if (PropertyA != null)
                writer.WriteElementString("PropertyA", XmlNamespace, PropertyA);
            writer.WriteStartElement("PropertyB", XmlNamespace);
            writer.WriteValue(PropertyB);
            writer.WriteEndElement();
        }
    
        #endregion
    }
    

    Here I have embedded the expected schema as a string literal inside the type. Alternatively you could load it from disk or build it by reflection.