Question

this my code

private DataTable ParseTable(string html)
    {
        HtmlDocument doc = new HtmlDocument();
        DataTable dt = new DataTable();
        String[] datasc;
        String[] valueTemp = new String[30];
        int index;

        doc.LoadHtml("<table><tr><td><p><input id=\"ControlGroupScheduleSelectView_AvailabilityInputScheduleSelectView_RadioButtonMkt1Fare7\" type=\"radio\" name=\"ControlGroupScheduleSelectView$AvailabilityInputScheduleSelectView$market1\" value=\"0~N~~N~RGFR~~1~X|QG~ 885~ ~~BTH~05/19/2014 07:00~KNO~05/19/2014 08:20~\" />Rp.445,000 ( N/Cls;4 )</p></td></tr></table>");

        for (int z = 0; z < 4; z++)
        {
            var getInputSchedule = doc.DocumentNode.SelectNodes("//table//input");
            datasc = new String[getInputSchedule.Count];

            for (int i = 0; i < getInputSchedule.Count; i = i+1)
            {
                string removeClassFare = string.Empty;
                String[] selectValueSplit = getInputSchedule[i].Attributes["value"].Value.Split('|');
                valueTemp[i] = selectValueSplit[1];
                String[] getAlphaSC = selectValueSplit[0].Split('~');

                try
                {
                    index = getInputSchedule[i].ParentNode.InnerText.IndexOf("(");
                    if (index != -1)
                    {
                        removeClassFare = getInputSchedule[i].ParentNode.InnerText.Substring(0, index);
                        removeClassFare = System.Text.Encoding.ASCII.GetString(System.Text.Encoding.ASCII.GetBytes(removeClassFare)).Replace("??", "").Replace("Rp.", "").Trim();
                    }
                }
                catch (Exception e) {
                    //removeClassFare = getInputSchedule[i].ParentNode.InnerText;
                }


                if (!dt.Columns.Contains(getAlphaSC[1]))
                {
                    dt.Columns.Add(getAlphaSC[1], typeof(string));
                }

                if (i == 0)
                {
                    datasc[i] = "<div align=\"center\"><input <input onclick='faredetail(this.value, this.name)' id=\"" + getInputSchedule[i].Attributes["id"].Value + "\" type=\"radio\" value=\"" + getInputSchedule[i].Attributes["value"].Value + "\" name=\"" + getInputSchedule[i].Attributes["name"].Value + "\"><br>" + removeClassFare + "</div>";
                }
                else
                {
                    if (selectValueSplit[1].Equals(valueTemp[i - 1],StringComparison.Ordinal))
                    {
                        datasc[i] = "<div align=\"center\"><input <input onclick='faredetail(this.value, this.name)' id=\"" + getInputSchedule[i].Attributes["id"].Value + "\" type=\"radio\" value=\"" + getInputSchedule[i].Attributes["value"].Value + "\" name=\"" + getInputSchedule[i].Attributes["name"].Value + "\"><br>" + removeClassFare + "</div>";
                    }
                    else
                    {
                        break;
                    }
                }
                getInputSchedule[i].Remove();
            }

            datasc = datasc.Where(x => !string.IsNullOrEmpty(x)).ToArray();
            dt.Rows.Add(datasc);
        }

        return dt;
    }

if i run, error message "Object reference not set to an instance of an object.", but if i remove the ID of element like

doc.LoadHtml("<table><tr><td><p><input type=\"radio\" name=\"ControlGroupScheduleSelectView$AvailabilityInputScheduleSelectView$market1\" value=\"0~N~~N~RGFR~~1~X|QG~ 885~ ~~BTH~05/19/2014 07:00~KNO~05/19/2014 08:20~\">Rp.445,000 ( N/Cls;4 )</p></td></tr></table>");

Everything works ok. Why does the ID attribute cause my XPath to fail?

pleasee..help..

thank you

Was it helpful?

Solution

I stand corrected. SelectNodes does return null if it can't find any nodes.

But the behavior you are witnessing has nothing to do with the id attribute (in fact, removing the id attribute causes an exception to happen sooner), and everything to do with your code.

At the end of your inner loop, you are doing this:

getInputSchedule[i].Remove();

which removes the <input> element from the HTML document.

Your outer loop is set up to execute four times, so the second time it executes, the input element is already gone, and doc.DocumentNode.SelectNodes("//table//input") returns null, and that is the cause of your error.

I'm not really sure why you're removing the input elements from the document as you go through it, or why you're looping through the whole thing 4 times, but hopefully that gets you going in the right direction.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top