Advertisement
Guest User

Untitled

a guest
May 26th, 2015
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.34 KB | None | 0 0
  1.  class Program
  2.     {
  3.         static void Main(string[] args)
  4.         {
  5.             var config = new Configuration().WithDefaultLoader();
  6.             var document = BrowsingContext.New(config).OpenAsync(Url.Create("http://www.w3.org/TR/css3-background/#ltboxgt")).Result;
  7.             Dictionary<string, string> atribute = new Dictionary<string, string>()
  8.                {
  9.                      {"Name","tr:nth-child(1) > td"},
  10.                      {"Value ","tr:nth-child(2) > td"}
  11.                };
  12.  
  13.             WriteAtributeToFile(document, atribute, "table.propdef tbody", @"D:\myOutput.csv");
  14.         }
  15.  
  16.         private static void WriteAtributeToFile(IDocument document, Dictionary<string, string> atribute, string selector, string file)
  17.         {
  18.             using (var stream = new StreamWriter(file))
  19.             {
  20.                 stream.WriteLine(string.Join(",", atribute.Select(a => a.Key)));
  21.                 foreach (var item in document.QuerySelectorAll(selector))
  22.                 {
  23.                     foreach (var i in atribute.Select(a => a.Value))
  24.                     {
  25.                         stream.Write(item.QuerySelector(i).TextContent.Replace("\n", "").Replace(",", ""));
  26.                         stream.Write(",");
  27.                     }
  28.                     stream.WriteLine();
  29.                 }
  30.             }
  31.         }
  32.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement