Raoull Duke
Raoull Duke

Reputation: 1

XMLReader skips every other node

I've been searching for similar problem, but every of those are simple to resolve (double reader.Read() method), but mine is very odd...

Here is my ASP.NET C# method:

private IEnumerable<XElement> getXMLData(String inputURI, String elementName)
    {
        List<XElement> result = new List<XElement>();
        XmlReader reader = XmlReader.Create(inputURI);
        XElement elem;

        reader.MoveToContent();
        while (reader.Read())
        {
            if (reader.NodeType == XmlNodeType.Element)
            {
                if (reader.Name.Equals(elementName))
                {
                    elem = XNode.ReadFrom(reader) as XElement;
                    if (elem != null)
                    {
                        result.Add(elem);
                    }
                }
            }
        }
        return result;
    }

I am trying to parse following URI: http://chartapi.finance.yahoo.com/instrument/1.0/aapl/chartdata;type=quote;range=1m/

And Im selecting elemName="p" - theese are values to my jqplot chart, by the way.

Problem is, that this parsing method saves to list every other "p" node; there are 20 "p" nodes in this XML file, but my parser only saves 10 element (every other, starts from first);

What am I doing wrong?

Upvotes: 0

Views: 266

Answers (1)

Lotok
Lotok

Reputation: 4607

A neater solution might look like this -

using (var client = new WebClient())
{
    string s = client.DownloadString("http://chartapi.finance.yahoo.com/instrument/1.0/aapl/chartdata;type=quote;range=1m/");
    var xDoc = XDocument.Parse(s);
    var result = xDoc.Descendants("p").ToList();

    return result;

}

replace the URL with the URL string passed into the method to make it more generic.

Upvotes: 2

Related Questions