首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >POX SVCUtil生成的类强制转换

POX SVCUtil生成的类强制转换
EN

Stack Overflow用户
提问于 2013-01-08 02:50:48
回答 2查看 556关注 0票数 0

我使用SVCUTIL从XSD生成一个类。我很难弄清楚如何获取传入的请求对象并从该对象中检索"MsgType“值。

我想通过这样做,我可以简单地使用以下命令来访问数据:

代码语言:javascript
复制
request.Request.MsgType

然而,事情并不是这么简单。request给我的唯一选项是: Equals GetHashCode GetSchema GetType Nodes ReadXML ToString WriteXML

为了访问MsgType,我需要对序列化对象进行某种类型的转换吗?

代码语言:javascript
复制
public ServiceProviderTic callRequestFunc(ServiceProviderTic request) {
      //How do I get request.Request.MsgType Value?
}

生成的类中的根元素:

代码语言:javascript
复制
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:

代码语言:javascript
复制
<ServiceProviderTic>
<Request>
<MsgType>act</MsgType>

XSD架构

代码语言:javascript
复制
<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"/>
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-01-10 08:41:01

经过几天的学习和弄清楚如何访问数据,而不是以旧的方式访问数据。这是我想出来的:

Microsoft提供了XSD和SVCUTIL,用于将xsd转换为类并使其可序列化。我被困在这个项目中的原因是因为复杂的类型,我以前从来没有这样做过。我使用:

命令提示符: XSD.exe ServiceProviderTic.xsd /CLASSES

它生成了ServiceProviderTic.cs

我创建了一个web服务:

接口:

代码语言:javascript
复制
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate="/MyService", RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Bare)]
    XElement callRequestFunc(XElement request);

类:

代码语言:javascript
复制
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的帮助函数。我还包含了额外的代码,这些代码删除了不需要的名称空间。

代码语言:javascript
复制
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)));
    }
}

我希望这对将来的人有帮助!

票数 0
EN

Stack Overflow用户

发布于 2013-01-08 04:29:36

查看一些关于创建同时支持XML和JSON的REST web服务(参见WebHttpBehavior类)的好建议here

除此之外,我不确定如何理解您的XSD。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14202173

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档