IDK
IDK

Reputation: 33

How to deserialize a string from an XML content?

I have an XML like this:

<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.

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

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.

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'.

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.

Any suggestions would be highly appreciated!

Thank you!

Upvotes: 0

Views: 47

Answers (1)

jdweng
jdweng

Reputation: 34429

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

}

Upvotes: 1

Related Questions