这是我的xml文件: http://goalserve.com/samples/soccer_inplay.xml
这是我的模特:
[XmlRoot(ElementName = "localteam")]
public class Localteam
{
[XmlAttribute(AttributeName = "name")]
public string Name { get; set; }
[XmlAttribute(AttributeName = "goals")]
public string Goals { get; set; }
[XmlAttribute(AttributeName = "id")]
public string Id { get; set; }
}
this is my controller:
XmlDocument doc = new XmlDocument();
List<Localteam> s = new List<Localteam>();
doc.Load(Server.MapPath("soccer_inplay.xml"));
foreach (XmlNode node in doc.SelectNodes("/scores/category/"))
{
s.Add(new Localteam
{
Name = node["Name"].InnerText
});
}
return View(s);
这是我的观点:
<table>
<tr>
<th>Name</th>
<th>Goals</th>
</tr>
@foreach (WebApplication22.Models.Localteam s in Model)
{
<tr>
<td>@s.Name</td>
<td>@s.Goals</td>
</tr>
}
</table>
我想显示这个xml feed,但我不能,我的视图有问题
答案 0 :(得分:0)
使用xml linq
using System.Collections.ObjectModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication57
{
class Program
{
const string URL = "http://goalserve.com/samples/soccer_inplay.xml";
static void Main(string[] args)
{
XDocument doc = XDocument.Load(URL);
Localteam.teams = doc.Descendants("odd").Select(x => new Localteam()
{
Name = (string)x.Attribute("name"),
games = x.Elements("type").Where(y => y.Attribute("id") != null).Select(y => new Game() {
id = (long)y.Attribute("id"),
opponent = (string)y.Attribute("name"),
suspend = (int)y.Attribute("suspend"),
odd = (Single)y.Attribute("odd"),
_type = (string)y.Attribute("type")
}).ToList()
}).ToList();
}
}
public class Localteam
{
public static List<Localteam> teams = new List<Localteam>();
public string Name { get; set; }
public List<Game> games { get; set; }
}
public class Game
{
public string opponent { get; set; }
public long id { get; set; }
public int suspend { get; set; }
public Single odd { get; set; }
public string _type { get; set; }
}
}