简体   繁体   中英

C# Nested Loops

I'm having trouble to make some loops.

I'm using agilitypack. I have a TXT file with several links (1 per line), and for each link that txt want to navigate to the page and then later extract to be in xpath and write in a memo.

The problem I'm having and that the code is only carrying out the procedure for the last line of txt. Where am I wrong?

var Webget = new HtmlWeb();
foreach (string line in File.ReadLines("c:\\test.txt"))
{
    var doc = Webget.Load(line);
    foreach (HtmlNode node in doc.DocumentNode.SelectNodes("//*[@id='title-article']"))
    {
        memoEdit1.Text = node.ChildNodes[0].InnerHtml + "\r\n";
        break;
    }
}

try to change

memoEdit1.Text = node.ChildNodes[0].InnerHtml + "\r\n";

to

memoEdit1.Text += node.ChildNodes[0].InnerHtml + "\r\n";

You're overwriting memoEdit1.Text every time. Try

memoEdit1.Text += node.ChildNodes[0].InnerHtml + "\r\n";

instead - note the += instead of = , which adds the new text every time.

Incidentally, constantly appending strings together isn't really the best way. Something like this might be better:

var Webget = new HtmlWeb();
var builder = new StringBuilder();
foreach (string line in File.ReadLines("c:\\test.txt"))
{
    var doc = Webget.Load(line);
    foreach (HtmlNode node in doc.DocumentNode.SelectNodes("//*[@id='title-article']"))
    {
        builder.AppendFormat("{0}\r\n", node.ChildNodes[0].InnerHtml);
        break;
    }
}
memoEdit1.Text = builder.ToString();

Or, using LINQ:

var Webget = new HtmlWeb();
memoEdit1.Text = string.Join(
    "\r\n",
    File.ReadAllLines("c:\\test.txt")
      .Select (line => Webget.Load(line).DocumentNode.SelectNodes("//*[@id='title-article']").First().ChildNodes[0].InnerHtml));

If you are only selecting 1 node in the inner loop then use SelectSingleNode Instead. Also the better practice when concatenating strings in a loop is to use StringBuilder :

StringBuilder builder = new StringBuilder();

var Webget = new HtmlWeb();
foreach (string line in File.ReadLines("c:\\test.txt"))
{
    var doc = Webget.Load(line);
    builder.AppendLine(doc.DocumentNode.SelectSingleNode("//*[@id='title-article']").InnerHtml);
}

memoEdit1.Text = builder.ToString();

Using linq it will look like this:

var Webget = new HtmlWeb();
var result = File.ReadLines("c:\\test.txt")
    .Select(line => Webget.Load(line).DocumentNode.SelectSingleNode("//*[@id='title-article']").InnerHtml));

memoEdit1.Text = string.Join(Environment.NewLine, result);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM