简体   繁体   English

如何反序列化 XML 内容中的字符串?

[英]How to deserialize a string from an XML content?

I have an XML like this:我有一个这样的 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> 

and the goal is to deserialize it into C# classes.目标是将其反序列化为 C# 个类。

My classes are as such:我的课程是这样的:

   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; }

    }
   }

also:还:

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; }

 }
}

I also have the classes: Odd , Match and Bet我也有课程: OddMatchBet

This is my code:这是我的代码:

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; 
     }

but on this line但在这条线上

sport = (Sport)ser.Deserialize(reader);

it gives me the following error:它给了我以下错误:

InnerException = {"<XmlSports xmlns=''> was not expected."}

I think it is because I've had created a class XmlSports but I removed it because when I run code first migration with Entity Framework, it said that there was an error because it couldn't create a primary key for the table XmlSports, so I removed this class and made the Sport class the root class instead.我认为这是因为我已经创建了一个 class XmlSports但我删除了它,因为当我使用 Entity Framework 运行代码优先迁移时,它说有一个错误,因为它无法为表 XmlSports 创建主键,所以我删除了这个 class 并将Sport class 改为根 class。

I tried to create an xsd file from this xml (I created a test.xml file and copied and pasted the content from the url) but it didn't happen because it said that: There was an error processing 'test.xml'.我试图从这个 883812976388 创建一个 xsd 文件(我创建了一个 test.xml 文件并复制并粘贴了 url 中的内容)但它没有发生,因为它说:处理'test.xml'时出错。

I don't have experience with xml content and serialization and deserialization in general so it's pretty hard for me to understand what's wrong and how to fix it.我对 xml 内容以及一般的序列化和反序列化没有经验,所以我很难理解问题出在哪里以及如何修复它。

Any suggestions would be highly appreciated!任何建议将不胜感激!

Thank you!谢谢!

This should get you started.这应该让你开始。 If you need more help ask如果您需要更多帮助,请询问

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM