将自定义类数组放入数据集或XML(C#)

将自定义类数组放入数据集或XML(C#),c#,xml,arrays,dataset,C#,Xml,Arrays,Dataset,我正在制作一个小纸牌游戏,它需要一个高分列表,保存到一个外部文件中,并在每个游戏开始时从中加载 我用以下格式编写了一个XML文件: <highscore> <name>bob</name> <score>10</score> <time>3:42</time> <date>21-09-09</date> </highscore> 我还弄明白了如何检查游戏的高分是否高于数组中的

我正在制作一个小纸牌游戏,它需要一个高分列表,保存到一个外部文件中,并在每个游戏开始时从中加载

我用以下格式编写了一个XML文件:

<highscore>
<name>bob</name>
<score>10</score>
<time>3:42</time>
<date>21-09-09</date>
</highscore>
我还弄明白了如何检查游戏的高分是否高于数组中的最低分


我正在进行排序,但是如何将
HighScore[]
数组返回到数据集中,然后再返回到XML中,或者甚至从数组直接返回到数据集中?我试着用谷歌搜索它,但我没有找到我想要的。

你真的需要使用
数据集来序列化你的数组吗?如果只需要序列化数组,则可以使用简单的Xml序列化。下面是一个例子:

    [XmlRoot("highScore")]
    public class HighScore
    {
        [XmlElement("name")]
        public string Name { get; set; }
        [XmlElement("dateTime")]
        public DateTime Date { get; set; }
        [XmlElement("score")]
        public int Score { get; set; }
    }

    static void Main(string[] args)
    {

        IList<HighScore> highScores = new[] { 
            new HighScore {Name = "bob", Date = DateTime.Now, Score = 10 },
            new HighScore {Name = "john", Date = DateTime.Now, Score = 9 },
            new HighScore {Name = "maria", Date = DateTime.Now, Score = 28 }
        };


        // serializing Array
        XmlSerializer s = new XmlSerializer(typeof(HighScore[]));
        using (Stream st = new FileStream(@"c:\test.xml", FileMode.Create))
        {
            s.Serialize(st, highScores.ToArray());
        }

        // deserializing Array
        HighScore[] highScoresArray;
        using (Stream st = new FileStream(@"c:\test.xml", FileMode.Open))
        {
            highScoresArray = (HighScore[])s.Deserialize(st);
        }

        foreach (var highScore in highScoresArray)
        {
            Console.WriteLine("{0}, {1}, {2} ", highScore.Name, highScore.Date, highScore.Score);
        }
    }
[XmlRoot(“高分”)]
公共课高分
{
[XmlElement(“名称”)]
公共字符串名称{get;set;}
[XmlElement(“日期时间”)]
公共日期时间日期{get;set;}
[XmlElement(“分数”)]
公共整数分数{get;set;}
}
静态void Main(字符串[]参数)
{
IList highScores=新[]{
新的高分{Name=“bob”,日期=日期时间。现在,分数=10},
新的高分{Name=“john”,Date=DateTime。现在,分数=9},
新的高分{Name=“maria”,日期=日期时间。现在,分数=28}
};
//序列化数组
XmlSerializer s=新的XmlSerializer(typeof(HighScore[]);
使用(Stream st=newfilestream(@“c:\test.xml”,FileMode.Create))
{
s、 序列化(st,highScores.ToArray());
}
//反序列化数组
HighScore[]highScoresArray;
使用(Stream st=newfilestream(@“c:\test.xml”,FileMode.Open))
{
highScoresArray=(HighScore[])s.反序列化(st);
}
foreach(highScoresArray中的var highScore)
{
WriteLine(“{0},{1},{2}”,highScore.Name,highScore.Date,highScore.Score);
}
}

您真的需要使用
数据集来序列化数组吗?如果只需要序列化数组,则可以使用简单的Xml序列化。下面是一个例子:

    [XmlRoot("highScore")]
    public class HighScore
    {
        [XmlElement("name")]
        public string Name { get; set; }
        [XmlElement("dateTime")]
        public DateTime Date { get; set; }
        [XmlElement("score")]
        public int Score { get; set; }
    }

    static void Main(string[] args)
    {

        IList<HighScore> highScores = new[] { 
            new HighScore {Name = "bob", Date = DateTime.Now, Score = 10 },
            new HighScore {Name = "john", Date = DateTime.Now, Score = 9 },
            new HighScore {Name = "maria", Date = DateTime.Now, Score = 28 }
        };


        // serializing Array
        XmlSerializer s = new XmlSerializer(typeof(HighScore[]));
        using (Stream st = new FileStream(@"c:\test.xml", FileMode.Create))
        {
            s.Serialize(st, highScores.ToArray());
        }

        // deserializing Array
        HighScore[] highScoresArray;
        using (Stream st = new FileStream(@"c:\test.xml", FileMode.Open))
        {
            highScoresArray = (HighScore[])s.Deserialize(st);
        }

        foreach (var highScore in highScoresArray)
        {
            Console.WriteLine("{0}, {1}, {2} ", highScore.Name, highScore.Date, highScore.Score);
        }
    }
[XmlRoot(“高分”)]
公共课高分
{
[XmlElement(“名称”)]
公共字符串名称{get;set;}
[XmlElement(“日期时间”)]
公共日期时间日期{get;set;}
[XmlElement(“分数”)]
公共整数分数{get;set;}
}
静态void Main(字符串[]参数)
{
IList highScores=新[]{
新的高分{Name=“bob”,日期=日期时间。现在,分数=10},
新的高分{Name=“john”,Date=DateTime。现在,分数=9},
新的高分{Name=“maria”,日期=日期时间。现在,分数=28}
};
//序列化数组
XmlSerializer s=新的XmlSerializer(typeof(HighScore[]);
使用(Stream st=newfilestream(@“c:\test.xml”,FileMode.Create))
{
s、 序列化(st,highScores.ToArray());
}
//反序列化数组
HighScore[]highScoresArray;
使用(Stream st=newfilestream(@“c:\test.xml”,FileMode.Open))
{
highScoresArray=(HighScore[])s.反序列化(st);
}
foreach(highScoresArray中的var highScore)
{
WriteLine(“{0},{1},{2}”,highScore.Name,highScore.Date,highScore.Score);
}
}