.NET Xml反序列化,xsi:type属性出现问题/错误
本文关键字:问题 错误 属性 type 反序列化 Xml xsi NET | 更新日期: 2023-09-27 18:28:54
VS2008,.NET Framework 3.5
我们正在使用WebEx Xml API。下面是他们web服务的Xml响应示例,我正试图将其反序列化为.NET类。
<?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#类是相当棘手的,我不确定这是正确的方法