0

SVCUTILを使用して、XSDからクラスを生成しました。着信リクエストオブジェクトを取得する方法を理解し、オブジェクトから「MsgType」値を取得するのに問題があります。

これを行うことで、次のコマンドを使用するだけでデータにアクセスできるようになると思いました。

request.Request.MsgType

しかし、それはこれほど単純ではありません。'request'が提供する唯一のオプションは次のとおりです。EqualsGetHashCodeGetSchemaGetType Nodes ReadXML ToString WriteXML

MsgTypeにアクセスするために、シリアル化されたオブジェクトに対して行う必要のあるキャストの種類はありますか?

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

生成されたクラスのルート要素:

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スキーマ

<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"/>
4

2 に答える 2

0

XMLとJSONの両方をサポートするRESTWebサービスの作成に関するいくつかの良いアドバイスをここで確認してください(WebHttpBehaviorクラスを参照)

それ以外は、XSDをどうすればよいかわかりません。

于 2013-01-07T20:29:36.017 に答える
0

数日間学習し、昔ながらの方法でデータにアクセスする方法を理解した後。これが私が思いついたものです:

Microsoftは、xsdをクラスに変換し、それらをシリアル化できるようにするためのXSDおよびSVCUTILを提供しています。私がこのプロジェクトにこだわった理由は、複雑なタイプのためでした。これまでこれを行ったことがありません。私が使用した:

コマンドプロンプト:XSD.exe ServiceProviderTic.xsd / CLASSES

ServiceProviderTic.csを生成した

Webサービスを作成しました:

インターフェース:

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

クラス:

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は、プレーンな古いxml(POX)を受け入れ、プレーンな古いxmlで応答するのに役立ちました。以下は、xelementをシリアル化および逆シリアル化するヘルパー関数です。また、不要な名前空間を削除する追加のコードも含めました。

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

これが将来誰かに役立つことを願っています!

于 2013-01-10T00:41:00.987 に答える