When i’m trying to remove a childnode from my xpath i’m getting a weird error:-
System.ArgumentOutOfRangeException was unhandled
Message=Node “” was not found in the collection
I know there an issue with HAP childremoving but idk if they have fix it with the new release or not. My question is it my code that is wrong or is it HAP? In either way is there any way to get around that and remove those childnode?
Here is my code:-
List<MediNetScheme> medinetScheme = new List<MediNetScheme>();
HtmlDocument htdoc = new HtmlDocument();
htdoc.LoadHtml(results);
foreach (HtmlNode table in htdoc.DocumentNode.SelectNodes("//table[@class='list-medium']/tbody[1]/tr[@class]"))
{
string itemValue = string.Empty;
HtmlNode ansvarig =table.SelectSingleNode("//table[@class='list-medium']/tbody[1]/tr[@class]/td[4]");
table.RemoveChild(ansvarig, true);
itemValue = table.InnerText;
medinetScheme.Add(new MediNetScheme(){Datum=itemValue.Remove(15),Sections=itemValue.Remove(0,15)});
}
MediNetScheme.ItemsSource = medinetScheme;
Edit:-
My HTML document has a table with several rows that have this xpath :- “//table[@class=’list-medium’]/tbody1/tr[@class]”. Each row in this table have 5 columns td1…td[5]. In my first foreach loop i’m using selectnodes to get the HTMLcode of each row in the table. What i want to do is to get only the innertext from the first 3 td in each row, which means i need to get rid of td[4] and td[5] from each row. When i used your edited code, i was able to get rid of td[4] and td[5] in the first row but not other rows that follows the first row.
Here is a pic of my HTML:-
After few hours of testing different codes and ways to achive what i wanted, i figured it out.
But i have to thank vfportero for his answer and flag it as answer too.
The answer to the edited verion of my question is simply this code 😉
You can see that i omit RemoveChild method coz it was not doing what i wanted (plz read the edit of my question), and instead i used .ChildNodes.RemoveAt(int //the placeof the child you want to remove).
Hope this will help some other ppl facing the same problem.
Yours