Xml选择新元素

时间:2014-04-11 23:36:47

标签: c# xml xna

我需要能够从select块返回数据。我该怎么做?

public double loadData()
    {
        XDocument loadedData = XDocument.Load("XMLFILE1.xml");
        var data = from query in loadedData.Descendants("ScoreData")
                   select new Score
                   {
                      HighScore = (Double)query.Element("HS"),
                   };

        return highScore;
    }

1 个答案:

答案 0 :(得分:1)

如果您需要单个值,那么为什么要创建Score实例?

这将返回First分数值:

return (from query in loadedData.Descendants("ScoreData")
               select  (Double)query.Element("HS")).First();

这将返回Maximum分数值:

return (from query in loadedData.Descendants("ScoreData")
               select  (Double)query.Element("HS")).Max();

如果您需要获得所有分数,则应将返回类型更改为double[]

public double[] loadData()
{
    XDocument loadedData = XDocument.Load("XMLFILE1.xml");

    return (from query in loadedData.Descendants("ScoreData")
                 select (Double)query.Element("HS")).ToArray();
}