如何在c# WPF应用程序中解析Xml

时间:2021-11-01 16:55:17

The UTF don't seem to work on my code. It is saying error in line 125.

UTF似乎不能处理我的代码。第125行是错误。

<feed>

</feed>

I have tried specifying what utf am using but dont seem to work

我已经尝试过指定utf在使用什么,但似乎没有效果

  var results = XElement.Load("games.xml")
    .Element("content.items")
    .Element("content.head")
    .Element("content.body")
    .Elements("results")
    .Elements("result");

var query = (from result in results
    let homeTeam = result.Element("home-team")
    let awayTeam = result.Element("away-team")
    let lastScorer = result.Descendants("scorers").Elements("scorer").OrderByDescending(p => (int)p.Attribute("time")).FirstOrDefault()
    select new
    {
          HomeTeam = homeTeam.Element("team-name").Value,
          AwayTeam = awayTeam.Element("team-name").Value,

          HomeTeamScore = homeTeam.Element("score").Value,
          AwayTeamScore = awayTeam.Element("score").Value,

          LastScorerName = lastScorer != null ? lastScorer.Element("player-name").Value : string.Empty,
          LastScoreTime = lastScorer != null ? (int?)lastScorer.Attribute("time") : null
    }).ToList();

1 个解决方案

#1


4  

Make your life easier by using Linq To XML.

使用Linq To XML使您的生活更轻松。

The following code just work and is more readable than using the low-level XmlReader.

下面的代码可以正常工作,并且比使用低级XmlReader更具可读性。

Assuming that the xml is stored in a file named games.xml.

假设xml存储在一个名为games.xml的文件中。

    var results = XElement.Load("games.xml")
        .Element("content.items")
        .Element("content.head")
        .Element("content.body")
        .Elements("results")
        .Elements("result");

    var query = (from result in results
        let homeTeam = result.Element("home-team")
        let awayTeam = result.Element("away-team")
        let lastScorer = result.Descendants("scorers").Elements("scorer").OrderByDescending(p => (int)p.Attribute("time")).FirstOrDefault()
        select new
        {
              HomeTeam = homeTeam.Element("team-name").Value,
              AwayTeam = awayTeam.Element("team-name").Value,

              HomeTeamScore = homeTeam.Element("score").Value,
              AwayTeamScore = awayTeam.Element("score").Value,

              LastScorerName = lastScorer != null ? lastScorer.Element("player-name").Value : string.Empty,
              LastScoreTime = lastScorer != null ? (int?)lastScorer.Attribute("time") : null
        }).ToList();

#1


4  

Make your life easier by using Linq To XML.

使用Linq To XML使您的生活更轻松。

The following code just work and is more readable than using the low-level XmlReader.

下面的代码可以正常工作,并且比使用低级XmlReader更具可读性。

Assuming that the xml is stored in a file named games.xml.

假设xml存储在一个名为games.xml的文件中。

    var results = XElement.Load("games.xml")
        .Element("content.items")
        .Element("content.head")
        .Element("content.body")
        .Elements("results")
        .Elements("result");

    var query = (from result in results
        let homeTeam = result.Element("home-team")
        let awayTeam = result.Element("away-team")
        let lastScorer = result.Descendants("scorers").Elements("scorer").OrderByDescending(p => (int)p.Attribute("time")).FirstOrDefault()
        select new
        {
              HomeTeam = homeTeam.Element("team-name").Value,
              AwayTeam = awayTeam.Element("team-name").Value,

              HomeTeamScore = homeTeam.Element("score").Value,
              AwayTeamScore = awayTeam.Element("score").Value,

              LastScorerName = lastScorer != null ? lastScorer.Element("player-name").Value : string.Empty,
              LastScoreTime = lastScorer != null ? (int?)lastScorer.Attribute("time") : null
        }).ToList();