gpt4 book ai didi

c# - .NET Xml 反序列化,xsi :type attribute 的问题/错误

转载 作者:行者123 更新时间:2023-12-04 16:56:32 25 4
gpt4 key购买 nike

VS2008,.NET 框架 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# 类非常棘手,我不确定这是正确的方法

关于c# - .NET Xml 反序列化,xsi :type attribute 的问题/错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9843655/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com