0

This is my xml file:

<categories>
   <category name="England: Fa Cup" id="1198" file_group="england">
     <matches date="Apr 17" formatted_date="17.04.2011">
       <match status="FT" date="Apr 17" formatted_date="17.04.2011" time="15:00" fix_id="440780" id="588118">
           <localteam name="Bolton" goals="0" id="9048" />
           <visitorteam name="Stoke" goals="5" id="9378" />
           <events>
              <event type="goal" minute="11" team="visitorteam" player="Etherington" result="[0 - 1]" playerId="2720" eventid="5881181" />
              <event type="goal" minute="17" team="visitorteam" player="Huth" result="[0 - 2]" playerId="11" eventid="5881182" />
              <event type="goal" minute="30" team="visitorteam" player="Jones" result="[0 - 3]" playerId="647" eventid="5881183" />
              <event type="goal" minute="68" team="visitorteam" player="Walters" result="[0 - 4]" playerId="15748" eventid="5881184" />
              <event type="goal" minute="81" team="visitorteam" player="Walters" result="[0 - 5]" playerId="15748" eventid="5881185" />
           </events>
           <ht score="[0-3]" />
       </match>
       <match status="FT" date="Apr 16" formatted_date="16.04.2011" time="16:15" fix_id="437322" id="588119">
          <localteam name="Manchester City" goals="1" id="9259" />
          <visitorteam name="Manchester Utd" goals="0" id="9260" />
          <events>
            <event type="goal" minute="52" team="localteam" player="Toure Y." result="[1 - 0]" playerId="4496" eventid="5881191" />
            <event type="redcard" minute="72" team="visitorteam" player="Scholes" result="" playerId="2915" eventid="5881192" />
          </events>
        <ht score="[0-0]" />
      </match>
      <match status="FT" date="Mar 13" formatted_date="13.03.2011" time="16:45" fix_id="364779" id="577376">
          <localteam name="Manchester City" goals="1" id="9259" />
          <visitorteam name="Reading" goals="0" id="9324" />
          <events>
             <event type="goal" minute="74" team="localteam" player="Richards" result="[1 - 0]" playerId="2852" eventid="5773761" />
          </events>
          <ht score="[0-0]" />
      </match>
    </matches>
  </category>
  <category name="England: Fa Trophy" id="1196" file_group="england">
    <matches date="May 07" formatted_date="07.05.2011">
      <match status="AET" date="May 07" formatted_date="07.05.2011" time="14:00" fix_id="441112" id="593257">
        <localteam name="Darlington" goals="1" id="9130" />
        <visitorteam name="Mansfield" goals="0" id="9262" />
        <events>
           <event type="goal" minute="120" team="localteam" player="Senior" result="[1 - 0]" playerId="35858" eventid="5932571" />
        </events>
        <ht score="[0-0]" />
     </match>
     <match status="FT" date="Mar 19" formatted_date="19.03.2011" time="15:00" fix_id="403865" id="584799">
       <localteam name="Gateshead" goals="0" id="9178" />
       <visitorteam name="Darlington" goals="0" id="9130" />
      <events></events>
      <ht score="[0-0]" />
    </match>
    <match status="AET" date="Mar 19" formatted_date="19.03.2011" time="15:00" fix_id="435610" id="583846">
       <localteam name="Luton" goals="1" id="9253" />
       <visitorteam name="Mansfield" goals="1" id="9262" />
       <events>
         <event type="goal" minute="46" team="localteam" player="Owusu" result="[1 - 0]" playerId="51213" eventid="5838461" />
         <event type="redcard" minute="84" team="localteam" player="Gnakpa" result="" playerId="53534" eventid="5838462" />
         <event type="redcard" minute="89" team="localteam" player="Lawless" result="" playerId="36238" eventid="5838463" />
          <event type="goal" minute="118" team="visitorteam" player="Briscoe" result="[1 - 1]" playerId="76789" eventid="5838464" />
        </events>
        <ht score="[0-0]" />
    </match>
  </matches>
 </category>
</categories>

My model

public class Category
 {
    public string Name { get; set; }      
    public int Id { get; set; }
    public string Filegroup { get; set; }
    public Matches Games{ get; set; }
 }

public class Matches
{
    public string Date { get; set; }
    public string Formatteddate { get; set; }
    public Observablecollection<Match> listGames{ get; set; }
}

public class Match
{
    public string Status { get; set; }
    public string Date { get; set; }
    public string Formatteddate { get; set; }
    public string Time { get; set; }
    public int Staticid { get; set; }
    public int Fixid { get; set; }
    public int Id { get; set; }
    public Localteam Homelteam { get; set; }
    public Visitorteam Awayteam { get; set; }
    public Events Ev { get; set; }
    public Ht HalfTime { get; set; }
}

public class Localteam
{
    public string Name { get; set; }
    public int Goals { get; set; }
    public int Id { get; set; }
}

public class Visitorteam
{
    public string Name { get; set; }
    public int Goals { get; set; }
    public int Id { get; set; }
}

public class Event
{
    public string Type { get; set; }
    public int Minute { get; set; }
    public string Team { get; set; }
    public string Player { get; set; }
    public string Result { get; set; }
    public string Playerid { get; set; }
    public int Eventid { get; set; }
}

public class Events
{
    public Observablecollection<Event> Ev { get; set; }
}

public class Ht
{
    public string Score { get; set; }
}

What is the best way to deserialize this peculiar xml file using C# ?

I was trying xsd.exe. I've created xml schema but I don't know how to edit that schema to use observablecollection instead of simple array(which is used by default).

Other option I'm considering is to convert that xml to json.

I've tried to deserialize that file using Json.NET but I'm getting error.

 Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type   'System.Collections.Generic.List`1[Testapp.Model.Match]' because the type requires a   JSON array (e.g. [1,2,3]) to deserialize correctly.

It seems like that post comes with good solution Cannot deserialize JSON array into type - Json.NET

I think that JsonConverter might do the trick like in the post above. But I have trouble to figure out how it might looks like for such a deep nested xml file.

I would appreciate any help.

Community
  • 1
  • 1
rood
  • 11,073
  • 3
  • 19
  • 18

0 Answers0