0

I have a schema I am trying to compile into data contracts. I have found if an element is defined as <xs:element name="DogRequest" type="Dog"></xs:element> no class is generated for DogRequest. I would like to use svcutil as I have multiple namespaces to generate, and xsd.exe only allows one. Plus I have a few elements using the same type, and xsd.exe only generates one of them. Does anyone know if there is a way to generate classes for this schema?

I'm using a generic web service that takes an XML payload. I am hoping use WCF to still build the message.

Schema

<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="XMLSchema1"
    targetNamespace="http://tempuri.org/XMLSchema1.xsd"
    elementFormDefault="qualified"
    xmlns="http://tempuri.org/XMLSchema1.xsd"
    xmlns:mstns="http://tempuri.org/XMLSchema1.xsd"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
>
  <xs:complexType name="Dog">
    <xs:sequence>
      <xs:element name="Name" type="xs:string"></xs:element>
    </xs:sequence>
  </xs:complexType>

  <xs:element name="DogRequest" type="Dog"></xs:element>
</xs:schema>

Compiled with svcutil /dconly XMLSchema1.xsd

This will generate 1 class for Dog, but nothing for DogRequest.

xsd.exe will generate 1 class for Dog with DogRequest

svcutil output

[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "4.0.0.0")]
[System.Runtime.Serialization.DataContractAttribute(Name="Dog", Namespace="http://tempuri.org/XMLSchema1.xsd")]
public partial class Dog : object, System.Runtime.Serialization.IExtensibleDataObject
{

    private System.Runtime.Serialization.ExtensionDataObject extensionDataField;

    private string NameField;

    public System.Runtime.Serialization.ExtensionDataObject ExtensionData
    {
        get
        {
            return this.extensionDataField;
        }
        set
        {
            this.extensionDataField = value;
        }
    }

    [System.Runtime.Serialization.DataMemberAttribute(IsRequired=true, EmitDefaultValue=false)]
    public string Name
    {
        get
        {
            return this.NameField;
        }
        set
        {
            this.NameField = value;
        }
    }
}

XSD Output

[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.17929")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://tempuri.org/XMLSchema1.xsd")]
[System.Xml.Serialization.XmlRootAttribute("DogRequest", Namespace="http://tempuri.org/XMLSchema1.xsd", IsNullable=false)]
public partial class Dog {

    private string nameField;

    /// <remarks/>
    public string Name {
        get {
            return this.nameField;
        }
        set {
            this.nameField = value;
        }
    }
}
Iain
  • 582
  • 5
  • 12

1 Answers1

1

Your understanding of the WSDL is incorrect. DogRequest is not a type, which is why no class is created. A schema <element> defines an xml element that can be used in other parts of the XSD/WSDL to reference the Dog complex type.

For example your WSDL probably has a section that says something like:

<message name="DogMessage">
    <part name="parameter" element="tns:DogRequest"/>
</message>

where tns is your target namespace. See the Types section of Understanding WSDL for more details.

When the XSD tool adds the XmlRootAttribute("DogRequest") to the Dog class, it is defining that the <DogRequest> element may be the document root of the xml message and should be serialized/deserialized to/from the Dog class.

From XmlRootAttribute:

The XmlRootAttribute allows you to control how the XmlSerializer generates the root element by setting certain properties. For example, specify the name of the generated XML element by setting the ElementName property.

ErnieL
  • 5,773
  • 1
  • 23
  • 27
  • I'm not using a WSDL, as the web service I'm hitting using a generic method to take an XML payload. I've found if I replace type="..." with it does generate a class. Does that make sense? – Iain Feb 19 '15 at 11:34
  • `` defines an inheritance relationship. Does it make sense that it generates a class? Yes. Question for you: Does it make sense that the `Dog` class inherits from `DogRequest`? Also be warned: Only XmlSerializer will support class inheritance (Data Contract Serializer does not). – ErnieL Feb 19 '15 at 17:25
  • Why do you say Data Contract Serializer does not support inheritance? I have used it for that purpose before and I had no issues. The pattern being used is each request has a complex type like `DogRequestType`, and a `DogRequest` element to make it a root. Thanks for the answer that it is not possible, that has been helpful. – Iain Feb 22 '15 at 22:41