簡體   English   中英

.NET Xml反序列化,帶有xsi:type屬性的問題/錯誤

[英].NET Xml deserialization, issue/error with xsi:type attribute

VS2008,.NET Framework 3.5

我們正在使用WebEx Xml API。 這是他們試圖將Web服務反序列化為.NET類的示例Xml響應。

<?xml version="1.0" encoding="UTF-8"?>
<serv:message xmlns:serv="http://www.webex.com/schemas/2002/06/service" xmlns:com="http://www.webex.com/schemas/2002/06/common" 

xmlns:event="http://www.webex.com/schemas/2002/06/service/event"><serv:header><serv:response><serv:result>SUCCESS</serv:result><serv:gsbStatus>PRIMARY</s

erv:gsbStatus></serv:response></serv:header>
<serv:body>
<serv:bodyContent xsi:type="event:lstsummaryEventResponse" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<event:matchingRecords>
    <serv:total>2</serv:total>
    <serv:returned>2</serv:returned>
    <serv:startFrom>1</serv:startFrom>
</event:matchingRecords>
<event:event>
    <event:sessionKey>999999</event:sessionKey>
    <event:sessionName>Test Event 1</event:sessionName>
    <event:sessionType>129</event:sessionType>
    <event:hostWebExID>SomeName</event:hostWebExID>
    <event:startDate>03/28/2012 14:30:00</event:startDate>
    <event:endDate>03/28/2012 14:45:00</event:endDate>
    <event:timeZoneID>11</event:timeZoneID>
    <event:duration>15</event:duration>
    <event:description></event:description>
    <event:status>NOT_INPROGRESS</event:status>
    <event:panelists></event:panelists>
    <event:listStatus>PUBLIC</event:listStatus>
</event:event>
</serv:bodyContent>
</serv:body>
</serv:message>

這是我們要反序列化的類:

using System;
using System.Xml;
using System.Xml.Serialization;
using System.Xml.Schema;
using System.Collections.Generic;

namespace Masonite.MTier.WebEx
{
    [Serializable()]
    [XmlRoot("message", Namespace = "http://www.webex.com/schemas/2002/06/service")]
    public class lstsummaryEventResponsexx
    {
        [XmlNamespaceDeclarations]
        public XmlSerializerNamespaces xmlns = new XmlSerializerNamespaces();

        public lstsummaryEventResponsexx()
        {
            xmlns.Add("serv", "http://www.webex.com/schemas/2002/06/service");
            xmlns.Add("com", "http://www.webex.com/schemas/2002/06/common");
            xmlns.Add("event", "http://www.webex.com/schemas/2002/06/service/event");
        }


        [XmlElement(ElementName = "header")]
        public Header header { get; set; }

        [XmlElement(ElementName = "body")]
        public Body body { get; set; }


        [Serializable()]
        [XmlRoot("header")]
        public class Header
        {
            [XmlElement(ElementName = "response")]
            public Response response { get; set; }
        }


        [Serializable()]
        [XmlRoot("body")]
        [XmlInclude(typeof(lstsummaryEventResponse))]
        public class Body
        {
            [XmlElement(ElementName = "bodyContent", Form = XmlSchemaForm.Qualified)]
            public BodyContent bodyContent { get; set; }
        }

        [Serializable()]
        public class lstsummaryEventResponse
        {

        }

        [Serializable()]
        [XmlRoot("response")]
        public class Response
        {
            [XmlElement(ElementName = "result")]
            public string result { get; set; }

            [XmlElement(ElementName = "reason")]
            public string reason { get; set; }

            [XmlElement(ElementName = "gsbStatus")]
            public string gsbStatus { get; set; }

            [XmlElement(ElementName = "exceptionID")]
            public string exceptionID { get; set; }
        }


        [Serializable()]
        [XmlRoot("bodyContent")]
        public class BodyContent
        {            
            [XmlElement(ElementName = "matchingRecords", Namespace = "http://www.webex.com/schemas/2002/06/service/event")]
            public MatchingRecords matchingRecords { get; set; }

            [XmlElement(ElementName = "event", Namespace = "http://www.webex.com/schemas/2002/06/service/event")]
            public List<EventSummary> events { get; set; }
        }


        [Serializable()]
        [XmlRoot("matchingRecords")]
        public class MatchingRecords
        {
            [XmlElement(ElementName = "total", Namespace = "http://www.webex.com/schemas/2002/06/service")]
            public int total { get; set; }

            [XmlElement(ElementName = "returned", Namespace = "http://www.webex.com/schemas/2002/06/service")]
            public int returned { get; set; }

            [XmlElement(ElementName = "startFrom", Namespace = "http://www.webex.com/schemas/2002/06/service")]
            public int startFrom { get; set; }
        }


        [Serializable()]
        [XmlRoot("event")]
        public class EventSummary
        {
            [XmlElement(ElementName = "sessionKey")]
            public long sessionKey { get; set; }

            [XmlElement(ElementName = "sessionName")]
            public string sessionName { get; set; }

            [XmlElement(ElementName = "sessionType")]
            public int sessionType { get; set; }

            [XmlElement(ElementName = "hostWebExID")]
            public string hostWebExID { get; set; }

            [XmlElement(ElementName = "startDate")]
            public string startDate { get; set; }

            [XmlElement(ElementName = "endDate")]
            public string endDate { get; set; }

            [XmlElement(ElementName = "timeZoneID")]
            public int timeZoneID { get; set; }

            [XmlElement(ElementName = "duration")]
            public int duration { get; set; }

            [XmlElement(ElementName = "description")]
            public string description { get; set; }

            [XmlElement(ElementName = "status")]
            public string status { get; set; }

            [XmlElement(ElementName = "panelists")]
            public string panelists { get; set; }

            [XmlElement(ElementName = "listStatus")]
            public listingType listStatus { get; set; }
        }
    }
}

我收到的錯誤:

The specified type was not recognized: name='lstsummaryEventResponse', namespace='http://www.webex.com/schemas/2002/06/service/event', at <bodyContent xmlns='http://www.webex.com/schemas/2002/06/service'>  

我不確定如何為Deserialize方法提供類型lstsummaryEventResponse。 我在上面的類中使用該名稱添加了另一個可序列化的類,但得到相同的錯誤。 有什么想法嗎?

BodyContent的類型可以是event:lstsummaryEventResponse因此必須聲明相應的類,然后按如下所示裝飾BodyContent的聲明:

    [Serializable()] 
    [XmlRoot("bodyContent")] 
    [XmlInclude("lstsummaryEventResponse")]
    public class BodyContent {

    }

話雖如此,使用對應於任意XML的序列化創建C#類非常棘手,但我不確定這是正確的方法

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM