0

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"/>
user697698
  • 45
  • 8

2 Answers2

0

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.

Tommy Grovnes
  • 4,126
  • 2
  • 25
  • 40
  • There is no WSDL, just an XSD. Do you have any other suggestions on how to go about casting the xml to a class? or perhaps a better method of accomplishing what i'm looking to do? – user697698 Jan 08 '13 at 16:41
  • Have you been able to call this web service, and in that case how are you calling it? or are you just looking to deserialize the bit of XML above ? If not who wrote the web service ? The "normal" way of describing a service is through a WSDL. I can see the XSD describes the XML sample but it's not enough to generate a working proxy. – Tommy Grovnes Jan 08 '13 at 22:21
  • I'm using Firefox's "Poster" addon to post plain xml data to my webservice. I'm wanting to deserialize the object to retrieve data values from nodes in code "callRequestFunc". I was given the XSD only, I converted the XSD to a class using SVCUTIl, then created a WCF service, but when I post plain XML to the service, I have no idea how to retrieve the values from the nodes. – user697698 Jan 09 '13 at 00:58
  • So the code above is for the server side part of it all (i.e. the actual web service) ? Sorry I'm trying to get my head around this :) – Tommy Grovnes Jan 09 '13 at 01:45
  • Yes, it's the server side part that a client will be posting too. – user697698 Jan 09 '13 at 17:25
0

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!

John Saunders
  • 160,644
  • 26
  • 247
  • 397
user697698
  • 45
  • 8