[英]How to deserialize a string from an XML content?
我有一个这样的 XML:
<XmlSports CreateDate="2022-12-04T17:47:53.5569879Z">
<Sport Name="eSports" ID="2357">
<Event Name="NBA2K, NBA Blitz League" ID="66838" IsLive="false" CategoryID="9357">
<Match Name="LA Clippers (THA KID) Esports - Milwaukee Bucks (SHARPSHOOTER)" ID="2711992" StartDate="2022-12-04T17:36:00" MatchType="Live">
<Bet Name="Money Line" ID="43859970" IsLive="true">
<Odd Name="1" ID="297613016" Value="2.26"/>
<Odd Name="2" ID="297613021" Value="1.58"/>
</Bet>
<Bet Name="Spread" ID="43859969" IsLive="true">
<Odd Name="1" ID="297614398" Value="1.83" SpecialBetValue="2.5"/>
<Odd Name="2" ID="297614399" Value="1.90" SpecialBetValue="2.5"/>
</Bet>
<Bet Name="Total" ID="43859971" IsLive="true">
<Odd Name="Over" ID="297613741" Value="1.86" SpecialBetValue="140.5"/>
<Odd Name="Under" ID="297613740" Value="1.86" SpecialBetValue="140.5"/>
</Bet>
</Match>
</Event>
<Event Name="FIFA, GT League" ID="62647" IsLive="false" CategoryID="8212">
.
. **and so on, the content is too long to post it here**
.
</Sport>
</XmlSports>
目标是将其反序列化为 C# 个类。
我的课程是这样的:
using System.Xml.Serialization;
namespace BettingDataAPI.Models
{
[Serializable, XmlRoot("Sport")]
public class Sport
{
[XmlElement("ID")]
public int ID { get; set; }
[XmlElement("Name")]
public string Name { get; set; }
[XmlElement("Event")]
public List<Event> Events { get; set; }
}
}
还:
using System.Xml.Serialization;
namespace BettingDataAPI.Models
{
[XmlType("Event")]
public class Event
{
[XmlElement("ID")]
public int ID { get; set; }
[XmlElement("Name")]
public string Name { get; set; }
[XmlElement("CategoryID")]
public int CategoryID { get; set; }
[XmlElement("IsLive")]
public bool IsLive { get; set; }
[XmlElement("Match")]
public List<Match> Matches { get; set; }
}
}
我也有课程: Odd , Match和Bet
这是我的代码:
private static async Task<Sport> DeserializeXMLToObject()
{
private static readonly string xmlUrl = @"*here is the url*";
XmlSerializer ser = new XmlSerializer(typeof(Sport));
//WebClient client = new WebClient();
HttpClient client = new HttpClient();
HttpResponseMessage response = client.GetAsync(xmlUrl).Result;
string xml = "";
if (response.IsSuccessStatusCode)
{
xml = response.Content.ReadAsStringAsync().Result;
}
Sport sport = new Sport();
using(TextReader reader = new StringReader(xml))
{
sport = (Sport)ser.Deserialize(reader);
}
return sport;
}
但在这条线上
sport = (Sport)ser.Deserialize(reader);
它给了我以下错误:
InnerException = {"<XmlSports xmlns=''> was not expected."}
我认为这是因为我已经创建了一个 class XmlSports但我删除了它,因为当我使用 Entity Framework 运行代码优先迁移时,它说有一个错误,因为它无法为表 XmlSports 创建主键,所以我删除了这个 class 并将Sport class 改为根 class。
我试图从这个 883812976388 创建一个 xsd 文件(我创建了一个 test.xml 文件并复制并粘贴了 url 中的内容)但它没有发生,因为它说:处理'test.xml'时出错。
我对 xml 内容以及一般的序列化和反序列化没有经验,所以我很难理解问题出在哪里以及如何修复它。
任何建议将不胜感激!
谢谢!
这应该让你开始。 如果您需要更多帮助,请询问
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
namespace ConsoleApplication2
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
XmlReader reader = XmlReader.Create(FILENAME);
XmlSerializer serializer = new XmlSerializer(typeof(XmlSports));
XmlSports sports = (XmlSports)serializer.Deserialize(reader);
}
}
public class XmlSports
{
public Sport Sport { get; set; }
}
public class Sport
{
[XmlAttribute("ID")]
public int ID { get; set; }
[XmlAttribute("Name")]
public string Name { get; set; }
[XmlElement("Event")]
public List<Event> Events { get; set; }
}
public class Event
{
[XmlAttribute("ID")]
public int ID { get; set; }
[XmlAttribute("Name")]
public string Name { get; set; }
[XmlAttribute("CategoryID")]
public int CategoryID { get; set; }
[XmlAttribute("IsLive")]
public bool IsLive { get; set; }
[XmlElement("Match")]
public List<Match> Matches { get; set; }
}
public class Match
{ }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.