【发布时间】:2019-05-24 15:26:44
【问题描述】:
我目前正在尝试将 xml 字符串反序列化为 C# 对象。 我没有收到任何错误。列表中没有任何内容。
XML 包含在 URL 中。我已经使用 A xml 到 C# 对象转换器来创建对象。
这是反序列化代码:
public static List<Models.Betway.EventList> GetEvents()
{
string res = "";
Uri myURI = new Uri("http://feeds.betway.com/events?key=CEBA7C11&keywords=horse-racing,uk-and-ireland&and=true");
WebRequest webRequest = WebRequest.Create(myURI);
webRequest.Timeout = 2000;
using (WebResponse webResponse = webRequest.GetResponse())
{
using (Stream stream = webResponse.GetResponseStream())
{
StreamReader reader = new StreamReader(stream, Encoding.UTF8);
res = reader.ReadToEnd();
}
}
XmlSerializer serializer = new XmlSerializer(typeof(List<Models.Betway.EventList>), new XmlRootAttribute("EventList"));
StringReader stringReader = new StringReader(res);
List<Models.Betway.EventList> list = (List<Models.Betway.EventList>)serializer.Deserialize(stringReader);
return list;
}
这是我使用在线转换器创建的 Object 类模型:
using System;
using System.Xml.Serialization;
using System.Collections.Generic;
namespace tf.PriceService.Core.Models.Betway
{
[XmlRoot(ElementName = "Keyword")]
public class Keyword
{
[XmlAttribute(AttributeName = "type_cname")]
public string Type_cname { get; set; }
[XmlAttribute(AttributeName = "cname")]
public string Cname { get; set; }
[XmlText]
public string Text { get; set; }
}
[XmlRoot(ElementName = "Keywords")]
public class Keywords
{
[XmlElement(ElementName = "Keyword")]
public List<Keyword> Keyword { get; set; }
}
[XmlRoot(ElementName = "Outcome")]
public class Outcome
{
[XmlElement(ElementName = "Name")]
public string Name { get; set; }
[XmlElement(ElementName = "Quicklink")]
public string Quicklink { get; set; }
[XmlAttribute(AttributeName = "id")]
public string Id { get; set; }
[XmlAttribute(AttributeName = "index")]
public string Index { get; set; }
[XmlAttribute(AttributeName = "type_cname")]
public string Type_cname { get; set; }
[XmlAttribute(AttributeName = "cname")]
public string Cname { get; set; }
[XmlAttribute(AttributeName = "starting_price")]
public string Starting_price { get; set; }
[XmlAttribute(AttributeName = "price_num")]
public string Price_num { get; set; }
[XmlAttribute(AttributeName = "price_den")]
public string Price_den { get; set; }
[XmlAttribute(AttributeName = "price_dec")]
public string Price_dec { get; set; }
}
[XmlRoot(ElementName = "Market")]
public class Market
{
[XmlElement(ElementName = "Name")]
public string Name { get; set; }
[XmlElement(ElementName = "Outcome")]
public List<Outcome> Outcome { get; set; }
[XmlAttribute(AttributeName = "id")]
public string Id { get; set; }
[XmlAttribute(AttributeName = "index")]
public string Index { get; set; }
[XmlAttribute(AttributeName = "type_cname")]
public string Type_cname { get; set; }
[XmlAttribute(AttributeName = "cname")]
public string Cname { get; set; }
[XmlAttribute(AttributeName = "handicap")]
public string Handicap { get; set; }
[XmlAttribute(AttributeName = "situation_index")]
public string Situation_index { get; set; }
[XmlAttribute(AttributeName = "each_way_active")]
public string Each_way_active { get; set; }
[XmlAttribute(AttributeName = "each_way_fraction_den")]
public string Each_way_fraction_den { get; set; }
[XmlAttribute(AttributeName = "each_way_position")]
public string Each_way_position { get; set; }
[XmlAttribute(AttributeName = "tricast")]
public string Tricast { get; set; }
[XmlAttribute(AttributeName = "forecast")]
public string Forecast { get; set; }
[XmlAttribute(AttributeName = "interval_from")]
public string Interval_from { get; set; }
[XmlAttribute(AttributeName = "interval_to")]
public string Interval_to { get; set; }
}
[XmlRoot(ElementName = "Event")]
public class Event
{
[XmlElement(ElementName = "Name")]
public string Name { get; set; }
[XmlElement(ElementName = "Quicklink")]
public string Quicklink { get; set; }
[XmlElement(ElementName = "Keywords")]
public Keywords Keywords { get; set; }
[XmlElement(ElementName = "Market")]
public List<Market> Market { get; set; }
[XmlAttribute(AttributeName = "id")]
public string Id { get; set; }
[XmlAttribute(AttributeName = "cname")]
public string Cname { get; set; }
[XmlAttribute(AttributeName = "start_at")]
public string Start_at { get; set; }
[XmlAttribute(AttributeName = "home_team_cname")]
public string Home_team_cname { get; set; }
[XmlAttribute(AttributeName = "away_team_cname")]
public string Away_team_cname { get; set; }
[XmlAttribute(AttributeName = "started")]
public string Started { get; set; }
}
[XmlRoot(ElementName = "EventList")]
public class EventList
{
[XmlElement(ElementName = "Event")]
public List<Event> Event { get; set; }
}
}
如您所见,我将 EventList 声明为根。但它仍然没有反序列化。
【问题讨论】:
-
你能给我们一个你正在反序列化的 XML 的例子吗? 您查看过您要反序列化的 XML 的实际文本吗?
-
嗨,是的,转到网址:feeds.betway.com/…
-
请在您的问题中添加一个 XML 示例,以重现该问题。谢谢。
-
URL 给了我一个 403。我想帮助你。 “自己去寻找”不是对提供更多信息的请求很有帮助的答案。
-
它为我反序列化了 89 个元素;当你说“它仍然没有反序列化”时,你看到了什么?
标签: c# xml object serialization