0

Best method joining string inside Object ( HTMLcollection )? I using microsoft studio 2008, NET 3.5, with HTMLagilityPack

Here the HTML

<div class="content">
<ul>
    <li>Text that I want to scrape </li>
    <li>Text that I want to scrape </li>
    <li>Text that I want to scrape </li>
</ul>
</div>  

and here my code

var productfeature = 
    document.DocumentNode.SelectNodes("//div[@class='content']/ul/li");

if (productfeature != null)
{
    StringBuilder sb = new StringBuilder();
    foreach (HtmlNode node in productfeature)
    {
        //Make sure nothing {} inside title
        string safeint = node.InnerText.Replace("}", "").Replace("{",""); 
        sb.Append(safeint + "}"); //adding } for marking 
    }
}

Some article in here said, its better using string.join, buat I dont know how to doing this with object element

NB: I want something faster and light..

Kjartan
  • 17,127
  • 14
  • 67
  • 84
radiaku
  • 212
  • 1
  • 6
  • 17
  • 1
    Using `string.Join` would potentially result in clearer, shorter code, but under the hood it shouldn't perform noticeably different. – Servy Jan 07 '13 at 19:54
  • 5
    "faster and light" are pretty meaningless terms when used in context of accessing the DOM. Which is not fast and certainly not light. Avoid pointless optimizations. – Hans Passant Jan 07 '13 at 19:56
  • Both comments above are correct, but this will help you http://stackoverflow.com/questions/585860/string-join-vs-stringbuilder-which-is-faster – prospector Jan 07 '13 at 19:57
  • Side note: the whole thing you are doing is probably not the best idea - merging structured data into one string to potentially parse it back sounds not very optimal. Even if you have to do that using some existing serialization (i.e. JSON) may be more reliable. – Alexei Levenkov Jan 07 '13 at 19:58
  • @HansPassant That mean, I have to stuck with string builder? – radiaku Jan 07 '13 at 20:01
  • @Prospector thanks for links.. – radiaku Jan 07 '13 at 20:03
  • @radiaku No, it means that this shouldn't be where you look to speed up the code. Even if you make this one bit a lot faster, the actual effect on the whole program will not be noticeable since this makes up such a small portion of the total work. – Servy Jan 07 '13 at 20:03
  • @Prospector Note that the string builder's implementation has been completely changed since that method was written, so it doesn't really mean anything for versions of .NET 4+ (or 3.5+, I forget when the changed it). – Servy Jan 07 '13 at 20:05
  • @Servy Yes that true. I just wondering how to make this with string.Join, I think its more simple code but I dont know how to do it? – radiaku Jan 07 '13 at 20:05

1 Answers1

1

Here is an implementation using string.Join

string delimiter = safeint + "}";
string result = "{" + string.Join(delimiter,
    productfeature.Select(node => removeBraces(node.InnerText)).ToArray()) + "}";

public static string removeBraces(string value)
{
    return value.Replace("}", "").Replace("{", "");
}
Servy
  • 193,745
  • 23
  • 295
  • 406
  • I got so many error and is this `string delimiter = safeint + "}"; string result = "{" + string.Join(delimiter, productfeature.Select(node => removeBraces(node.InnerText))) + "}";` inside foreach? – radiaku Jan 07 '13 at 20:18
  • @radiaku No, it's in place of the `foreach`. The helper method will obviously need to go elsewhere. Oh, and I forgot that with .NET 3.5 you need an extra `ToArray`. – Servy Jan 07 '13 at 20:22
  • thanks man, I know its not getting my code more faster than before but I learn something new about string.join and object Marked for answers – radiaku Jan 07 '13 at 20:42