要做到這一點,最簡單的方法是使用LINQ到XML。類似這樣的:
var doc = XDocument.Load(url);
var tournaments = doc.Root
.Elements("category")
.Where(x => (string) x.Attribute("name") == "Tournament")
.Single(); // Is there only one matching catgeory?
var matches = tournaments
.Elements("match")
.Select(m => new
{
LocalTeam = (string) m.Element("localteam").Attribute("name"),
VisitorTeam = (string) m.Element("localteam").Attribute("name"),
Events = m.Elements("Events")
.Select(e => new
{
Player = (string) e.Attribute("player"),
Type = (string) e.Attribute("type"),
// etc
})
.ToList();
});
如何顯示,然後由您決定。您可能希望爲Event,Match等創建自己的「正常」類型,而不是使用上面的匿名類型。
LINQ to XML是迄今爲止我使用過的最簡單的XML工作方式。
@Jon ..Sry buddy..acktly m不是精通LINQ to XML ..是否有其他的方式來搜索xml ...像這樣... XmlNodeList xnodeMatches = xdoc.SelectNodes(「// category [@ name ='「+ tournament +'']/id [@ id ='」+ tournamentID +'']/match「); – vikas