Question

I used the SVCUTIL to generate a class form an XSD. I'm having difficulties figuring out how to take the incoming request object and retrieving the "MsgType" value from the object.

I thought by doing this I would be able to access the data simply using:

request.Request.MsgType

However, it isn't this simple. The only options 'request' gives me is: Equals GetHashCode GetSchema GetType Nodes ReadXML ToString WriteXML

Is there some sort of casting I need to do to the serialized object in order to access MsgType?

public ServiceProviderTic callRequestFunc(ServiceProviderTic request) {
      //How do I get request.Request.MsgType Value?
}

Root Element in generated class:

using System.Runtime.Serialization;

[assembly: System.Runtime.Serialization.ContractNamespaceAttribute("", ClrNamespace="")]

[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "3.0.0.0")]
[System.Runtime.Serialization.DataContractAttribute(Name="RequestType", Namespace="")]
[System.Runtime.Serialization.KnownTypeAttribute(typeof(ResponseType))]
[System.Runtime.Serialization.KnownTypeAttribute(typeof(DateTimeInfoType))]
[System.Runtime.Serialization.KnownTypeAttribute(typeof(OriginType))]
[System.Runtime.Serialization.KnownTypeAttribute(typeof(LocaleInfoType))]
[System.Runtime.Serialization.KnownTypeAttribute(typeof(ProductType))]
[System.Runtime.Serialization.KnownTypeAttribute(typeof(ValueType))]
[System.Runtime.Serialization.KnownTypeAttribute(typeof(AuthInfoType))]
public partial class RequestType : object, System.Runtime.Serialization.IExtensibleDataObject
{

private RequestType.MsgTypeType MsgTypeField;

[System.Runtime.Serialization.DataMemberAttribute(IsRequired=true)]
public RequestType.MsgTypeType MsgType
{
    get
    {
        return this.MsgTypeField;
    }
    set
    {
        this.MsgTypeField = value;
    }
}

 [System.Runtime.Serialization.DataContractAttribute(Name="RequestType.MsgTypeType", Namespace="")]
public enum MsgTypeType : int
{
    [System.Runtime.Serialization.EnumMemberAttribute()]
    act = 0
}
}

[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "3.0.0.0")]
[System.Xml.Serialization.XmlSchemaProviderAttribute("ExportSchema")]
[System.Xml.Serialization.XmlRootAttribute(IsNullable=false)]
public partial class ServiceProviderTic : object, System.Xml.Serialization.IXmlSerializable
{

private System.Xml.XmlNode[] nodesField;

private static System.Xml.XmlQualifiedName typeName = new System.Xml.XmlQualifiedName("ServiceProviderTic", "");

public System.Xml.XmlNode[] Nodes
{
    get
    {
        return this.nodesField;
    }
    set
    {
        this.nodesField = value;
    }
}

public void ReadXml(System.Xml.XmlReader reader)
{
    this.nodesField = System.Runtime.Serialization.XmlSerializableServices.ReadNodes(reader);
}

public void WriteXml(System.Xml.XmlWriter writer)
{
    System.Runtime.Serialization.XmlSerializableServices.WriteNodes(writer, this.Nodes);
}

public System.Xml.Schema.XmlSchema GetSchema()
{
    return null;
}

public static System.Xml.XmlQualifiedName ExportSchema(System.Xml.Schema.XmlSchemaSet schemas)
{
    System.Runtime.Serialization.XmlSerializableServices.AddDefaultSchema(schemas, typeName);
    return typeName;
}

XML:

<ServiceProviderTic>
<Request>
<MsgType>act</MsgType>

XSD Schema

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="ServiceProvideTic" nillable="false">
<xs:annotation>
<xs:documentation></xs:documentation>
</xs:annotation>
<xs:complexType>
<xs:sequence>
<xs:element name="Version" type="xs:string" nillable="false"/>
<xs:choice>
<xs:element name="Request" type="RequestType" nillable="false"/>
<xs:element name="Response" type="ResponseType" nillable="false"/>
</xs:choice>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="RequestType">
<xs:annotation>
<xs:documentation> Request Information</xs:documentation>
</xs:annotation>
<xs:sequence>
<xs:element name="MsgType" nillable="false">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="act"/>
Was it helpful?

Solution 2

After several days of learning and figuring out how to access the data without doing it the old fashion way. Here is what I have came up with:

Microsoft offers an XSD and SVCUTIL for converting xsd's to classes and making them serializable. The reason I was stuck on this project was because of the complex types, i've never done this before. I used:

Command Prompt: XSD.exe ServiceProviderTic.xsd /CLASSES

Which generated ServiceProviderTic.cs

I created a web service:

Interface:

[OperationContract]
[WebInvoke(Method = "POST", UriTemplate="/MyService", RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Bare)]
    XElement callRequestFunc(XElement request);

Class:

public XElement callRequestFunc(XElement request)
    {
        ServiceProviderTic requestSer = Utility.DeserializeData(request);

        if (requestSer.Item.GetType() == typeof(RequestType))
        {
            RequestType reqObj = (RequestType)requestSer.Item;
            string datapiece = reqObj.MsgType.ToString();
        }

        XElement responseSer = Utility.SerializeData(requestSer);

        return responseSer;
    }
}

XElement helped me accept plain old xml (POX) and respond with plain old xml. Below are the helper functions which serialize and deserialize my xelement. I also included extra code which removed namespaces I didn't need.

public class Utility
{

    public static ServiceProviderTic DeserializeData(XElement request)
    {
        var ser = new XmlSerializer(typeof(ServiceProviderTic));
        return (ServiceProviderTic)ser.Deserialize(request.CreateReader());
    }

    public static XElement SerializeData(ServiceProviderTic response)
    {
        using (var memoryStream = new MemoryStream())
        {
            using (TextWriter streamWriter = new StreamWriter(memoryStream))
            {
                var xmlSerializer = new XmlSerializer(typeof(ServiceProviderTic));
                xmlSerializer.Serialize(streamWriter, response);
                return Utility.RemoveAllNamespaces(XElement.Parse(Encoding.ASCII.GetString(memoryStream.ToArray())));
            }
        }
    }

    public static XElement RemoveAllNamespaces(XElement source)
    {
        return !source.HasElements
                   ? new XElement(source.Name.LocalName)
                   {
                       Value = source.Value
                   }
                   : new XElement(source.Name.LocalName, source.Elements().Select(el => RemoveAllNamespaces(el)));
    }
}

I hope this helps someone in the future!

OTHER TIPS

Check out some good advice here on creating REST web services which support both XML and JSON (see the WebHttpBehavior class)

Other than that I'm not sure what to make of your XSD.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top