这只是一种预感,但如果您尝试访问该URI更频繁地超过60秒,您会收到以下消息返回:
`Please use a minimum interval of 60 sec (please wait for 10,65625 sec).`
由于这回应显然不是XML,XmlReader
对此无能为力。
只要连接之间有60秒或更长时间,我就可以成功运行您的代码。
几件事情加:
你如果条件不会得到你正在寻找的节点 - XML是区分大小写的。它应该是这样的:
IF((reader.NodeType == XmlNodeType.Element)& &(reader.Name == “OO”))
的LINQ to XML将是一个更容易(在我记)这样的方式:
的LINQ to XML实例
using System.Xml.Linq;
// This loads the XML from the specified URI into the XDocument
XDocument xDoc = XDocument.Load("http://www.bet-at-home.com/oddxml.aspx");
// This query will return a collection of all the "OO" elements (and their children)
var oo = from x in xDoc.Descendants("OO")
select x;
// You can iterate through this collection and do what you want
foreach (XElement ele in oo)
{
// do something with each OO group
}
Alternati诚然,你可以从查询中返回一个匿名类型(或定义类型)。例如:
var oo = from x in xDoc.Descendants("OO")
select new
{
Sport = x.Element("Sport").Value,
Category = x.Element("Category").Value,
Tournament = x.Element("Tournament").Value,
Date = x.Element("Date").Value
// and so on
};
然后,您将收集可以迭代的这些匿名类型的集合。
如果你有一个类定义了你想要保存数据(比如BettingOdds),那么你只需要使用它,select new
就会变成select new BettingOdds
。
请注意,您需要检查以确保您所指的元素存在,否则您将收到空引用异常。
有一吨的你可以使用LINQ做什么,以XML在互联网上的例子 - 在这里是一个例子 - LINQ To XML Tutorials with Examples
来源
2013-06-01 01:56:01
Tim
什么是完整的错误?你为什么不使用LINQ to XML? –
未处理的异常:System.Xml.XmlException:根级别的数据无效。 行1,位置1在 System.Xml.XmlTextReaderImpl.Throw(例外五) 在System.Xml.XmlTextReaderImpl.Throw(字符串RES,字符串ARG) 在系统。Xml.XmlTextReaderImpl.ParseRootLevelWhitespace() 在System.Xml.XmlTextReaderImpl.ParseDocumentContent() 在System.Xml.XmlTextReaderImpl.Read() 在Bet_At_Home.Program.Main(字串[] args)在C:\用户\世界你好\ documents \ visual studio 2010 \ Projects \ ... – Axel
@Axel该错误表明我以前的评论不正确。它*是*提取*东西*(它也可能是一个错误响应!),但是提取的数据看起来是格式不正确的XML(包括HTML,它不是XML!)。 – user2246674