简体   繁体   中英

Trying to read xml from a file not ending with .xml

I am trying to read xml from a sport odds feed, but I can't get it to work. I am using xmlReader.Create(url), but all I get is a data at the root level is invalid -error. This is what the code look like:

        XmlReader reader = XmlReader.Create("http://www.bet-at-home.com/oddxml.aspx");

        while (reader.Read())
        {

            if ((reader.NodeType == XmlNodeType.Element) && (reader.Name == "oo"))
            {
                //bla bla
            }
        }

You need to make an httprequest to get the data. You can't just feed that method a url and expect it to treat it like a local file. First make an http request and save the the response in a string then you can process that like an xml. Here's a link to the HttpWebRequest for .NET http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.aspx

This is just a hunch, but if you try to access that URI more frequently than 60 seconds, you get the following message returned:

`Please use a minimum interval of 60 sec (please wait for 10,65625 sec).` 

Since this response is clearly not XML, the XmlReader can't do anything with it.

I'm able to successfully run your code as long as there's 60 seconds or more between connections.

A couple of things to add:

  1. Your if condition is not going to get the nodes you are looking for - XML is case-sensitive. It should be like this:

    if ((reader.NodeType == XmlNodeType.Element) && (reader.Name == "OO"))

  2. LINQ to XML would be an easier (in my mind) way of doing this:

LINQ to XML Example

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
}

Alternatively, you could return an anonymous type (or a defined type) from the query. For example:

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
         };

You would then have a collection of these anonymous types that you could iterate through.

If you had a class defined that you wanted to hold data in (say BettingOdds), you'd simply use that instead and the select new line would become select new BettingOdds .

Note that you'll need to check to ensure that the elements you're referring to exist, otherwise you'll get a null reference exception.

There's a ton of examples on what you can do with LINQ to XML on the internet - here's an example - LINQ To XML Tutorials with Examples

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM