0

这是我的 xml 文件:

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

我的模型

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

使用 C# 反序列化这个特殊的 xml 文件的最佳方法是什么?

我正在尝试 xsd.exe。我已经创建了 xml 架构,但我不知道如何编辑该架构以使用 observablecollection 而不是简单数组(默认情况下使用)。

我正在考虑的其他选择是将 xml 转换为 json。

我尝试使用 Json.NET 反序列化该文件,但出现错误。

 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.

似乎该帖子提供了很好的解决方案 无法将 JSON 数组反序列化为类型 - Json.NET

我认为 JsonConverter 可能会像上面的帖子那样做到这一点。但是我很难弄清楚这样一个深度嵌套的 xml 文件的外观。

我将不胜感激任何帮助。

4

0 回答 0