2

I am using HtmlAgilityPack to parse some data and writing statements like the below one to remove unwanted contents -

doc.DocumentNode.SelectNodes("//ul").ToList().ForEach(a => a.Remove());

This works well when there's <ul> element present in the HTML document. However, if there's no <ul> element, I get a Value cannot by null exception when converting it into .ToList().

I do not want to use the following -

doc.DocumentNode.SelectNodes("//ul")== null ? null : doc.DocumentNode.SelectNodes("//ul").ToList().ForEach(a => a.Remove());

What elegant alternatives do I have?

2
  • 1
    What do you find inelegant about your proposed solution? (Except, of course, that it wont compile! - ForEach is void and null on the other side won't coalesce) Commented Oct 16, 2015 at 9:25
  • @Jamiec, I know it won't compile and just wanted to share the way I don't want to write. This approach is OK when using on a single tag, but let's say I have around 100 tags and I want to remove them. lot of code to be written and I know I can transfer this into a function but that does not server the function properly as well. Commented Oct 16, 2015 at 9:33

4 Answers 4

6

In your example you are calling doc.DocumentNode.SelectNodes("//ul") twice. What is wrong with

var nodes = doc.DocumentNode.SelectNodes("//ul");
if(nodes != null)
   nodes.ToList().ForEach(a => a.Remove());
Sign up to request clarification or add additional context in comments.

7 Comments

Is remove within a foreach really recommended?
@Thomas it's what the OP is doing, and isn't what the question is about.
@Marius, I was wondering if this can be written in single statement. Please advise.
It can be in C# 6. But a statement should not be your goal. Your goal should be correct and simple code.
Well I have this answer right now, more to come after @Nitesh
|
4

If you have C# 6 available you can use The Null Conditional Operator:

doc.DocumentNode.SelectNodes("//ul")?.ToList().ForEach(a => a.Remove());

Comments

2

I would use RemoveAll instead of a ForEach (if you can use C#6):

doc.DocumentNode.SelectNodes("//ul")?.ToList().RemoveAll();

Or

var nodes = doc.DocumentNode.SelectNodes("//ul");
if(nodes != null)
    nodes.ToList().RemoveAll();

Comments

1

You need to check the null condition.

var docNodes = doc.DocumentNode.SelectNodes("//ul");
if(docNodes != null)
   docNodes .ToList().ForEach(a => a.Remove());

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.