如何在 C# 中将 xml 反序列化为列表

How to deserialize xml into list in c#

我想在包含属性和列表的嵌套 类 中取消实现此 XML:

<BetradarLiveOdds timestamp="1630916055900" status="meta">
    <Match active="0" booked="0" matchid="22546" status="ended">
        <MatchInfo>
            <DateOfMatch>16309114325</DateOfMatch>
            <Sport id="32">Bos</Sport>
            <Category id="266">Interonal</Category>
            <Tournament id="99000526">Grd Pix 221</Tournament>
            <HomeTeam id="45179">Mrk Roal</HomeTeam>
            <AwayTeam id="41325">Mathew Whrs</AwayTeam>
            <TvChannels />
        </MatchInfo>
        <Translation>
            <Sport id="32">
                <Name lang="en">Bols</Name>
                <Name lang="it">Bols</Name>
            </Sport>
            <Category id="266">
                <Name lang="en">Interonal</Name>
                <Name lang="it">Internanale</Name>
            </Category>
            <Tournament id="99000526">
                <Name lang="en">Grand Prix 2021</Name>
                <Name lang="it">Grand Prix 2021</Name>
            </Tournament>
            <HomeTeam id="45179">
                <Name lang="en">Mark Royal</Name>
                <Name lang="it">Mark Royal</Name>
            </HomeTeam>
            <AwayTeam id="41325">
                <Name lang="en">Matthew Whyers</Name>
                <Name lang="it">Matthew Whyers</Name>
            </AwayTeam>
        </Translation>
    </Match>
</BetradarLiveOdds>

我创建了多个 类 可序列化的,如下所示:

[Serializable()]
public class BetradarLiveOdds
{
    [XmlAttribute("timestamp")]
    public long Timestamp { get; set; }

    [XmlAttribute("status")]
    public string Status { get; set; }

    [XmlArrayItem("match")]
    public List<Match> Matchs { get; set; } 
}

[Serializable()]
public class Match
{
    [XmlAttribute("active")]
    public bool Active { get; set; }

    [XmlAttribute("booked")]
    public bool Booked { get; set; }

    [XmlAttribute("matchid")]
    public Int32 MatchId { get; set; }

    [XmlAttribute("status")]
    public string Status { get; set; }

    [XmlElement("matchinfo")]
    public MatchInfo MatchInfo { get; set; }

    [XmlElement("translation")]
    public List<Translation> Translations { get; set; } 
}

和其他 类(运动、锦标赛、主队...)。

我尝试:当我尝试反序列化时:

static void Main(string[] args)
{
    XmlSerializer serializer = new XmlSerializer(typeof(BetradarLiveOdds));
    BetradarLiveOdds bet;

    FileStream fs = new FileStream("C:\src\bet2020\RD_Live_Parsing\meta\hi.xml", FileMode.Open);
    bet = (BetradarLiveOdds)serializer.Deserialize(fs);

    serializer.Serialize(Console.Out, bet);
}

BetradarLiveOdds 打印正常但匹配为空。 有谁知道如何修改 类 或 main 使其工作。我怎样才能反序列化文件?

如果您使用以下 class 定义,那么您的反序列化器 + 序列化器代码将适用于单个或多个 <Match> 节点:

[XmlRoot]
public class BetradarLiveOdds
{
    [XmlElement]
    public Match[] Match { get; set; }

    [XmlAttribute(AttributeName = "timestamp")]
    public string Timestamp { get; set; }
    [XmlAttribute(AttributeName = "status")]
    public string Status { get; set; }
}
  • 我省略了所有不需要的属性
  • 如果您需要列表而不是数组,
  • XmlSerializer 也可以与 List<Match> 一起使用
public class Match
{
    public MatchInfo MatchInfo { get; set; }
    public Translation Translation { get; set; }

    [XmlAttribute(AttributeName = "active")]
    public string Active { get; set; }
    [XmlAttribute(AttributeName = "booked")]
    public string Booked { get; set; }
    [XmlAttribute(AttributeName = "matchid")]
    public string Matchid { get; set; }
    [XmlAttribute(AttributeName = "status")]
    public string Status { get; set; }
}
public class MatchInfo
{
    public string DateOfMatch { get; set; }
    public Common Sport { get; set; }
    public Common Category { get; set; }
    public Common Tournament { get; set; }
    public Common HomeTeam { get; set; }
    public Common AwayTeam { get; set; }
    public string TvChannels { get; set; }
}
  • 我介绍了一个 Common class 可以在 MatchInfoTranslation
  • 中使用
  • MatchInfo 的情况下,Name 集合将为空(具有 0 个元素的初始化列表)
    • 如果不希望这样,那么您可以引入另一个 class,它不会包含 Name 属性
public class Translation
{
    public Common Sport { get; set; }
    public Common Category { get; set; }
    public Common Tournament { get; set; }
    public Common HomeTeam { get; set; }
    public Common AwayTeam { get; set; }
}
public class Common
{
    [XmlAttribute(AttributeName = "id")]
    public string Id { get; set; }
    [XmlText]
    public string Text { get; set; }
    [XmlElement]
    public List<Name> Name { get; set; }
}
public class Name
{
    [XmlAttribute(AttributeName = "lang")]
    public string Lang { get; set; }
    [XmlText]
    public string Text { get; set; }
}