Find the Error

This code was written by me,in a late night moment,when copy paste seems the best option
It started this way :

public string ImportFeed(string URL)
        {
            string ret = "";
            XmlDocument xd = new XmlDocument();
            xd.Load(URL);            
            for(int i=0;i<xd.ChildNodes.Count;i++)
            {
                ret += xd.ChildNodes[i].Name;
            }
            return ret;
            
        }

So far,nothing special. Just open the url,read into an XML,return first nodes. Then I wanted more – next nodes. Nothing easier – why bother with recursion or proper variable names ? Just copy and paste :

public string ImportFeed(string URL)
        {
            string ret = "";
            XmlDocument xd = new XmlDocument();
            xd.Load(URL);            
            for(int i=0;i<xd.ChildNodes.Count;i++)
            {
                ret += xd.ChildNodes[i].Name;
                for (int j = 0; i < xd.ChildNodes[i].ChildNodes.Count; j++)
                {
                    ret += xd.ChildNodes[i].ChildNodes[j].Name;
                }
            }
            return ret;
            
        }

Could you spot the error ?
Hint : I have discovered after 10 seconds …but not looking at the code…


Posted

in

by

Tags:

Comments

7 responses to “Find the Error”

  1. matei Avatar
    matei

    the pb is here:
    for (int j = 0; i < xd.ChildNodes[i].ChildNodes.Count; j++)
    it should be j < , not i <

    1. Andrei Ignat Avatar
      Andrei Ignat

      right!

  2. Siderite Avatar

    Took me 5 secs, reading your post. 😉

    Come on, give us something tougher!

  3. Matthias Krells Avatar

    Wirklich Nice! i like it! Wo ist denn der Facebook-Like-Button 😉 Matthias

    1. Andrei Ignat Avatar
      Andrei Ignat

      I will post ASAP.

  4. Andrei Rinea Avatar

    Ei bine, cu rușine recunosc că nici după 30 sec nu m-am prins. M-am enervat și am derulat la comentarii :))

  5. ASP.NET Avatar

    na ja…. such indexes i, j, l could be dangerous 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *