How to use HTML Agility Pack in Lightswitch 2013 Desktop Application - c#

I added the HTML Agility Pack (HAP) to my solution using Nuget. My solution uses Silverlight 5. Some functions I can call from "using HtmlAgilityPack;" but some functions I cannot call. I need to get content of URL by following code lines:
string Url = "http://google.com.vn"; // this line is okay
HtmlWeb web = new HtmlWeb(); // this line is okay
HtmlDocument doc = web.Load(Url); // but this line is highlighted with an error 'Error HtmlAgilityPack.HtmlWeb' does not contain a definition for 'Load' and no extension method 'Load' accepting a first argument of type 'HtmlAgilityPack.HtmlWeb' could be found (are you missing a using directive or an assembly reference?)'

I solved my question
using System;
using System.Linq;
using System.IO;
using System.IO.IsolatedStorage;
using System.Collections.Generic;
using Microsoft.LightSwitch;
using Microsoft.LightSwitch.Framework.Client;
using Microsoft.LightSwitch.Presentation;
using Microsoft.LightSwitch.Presentation.Extensions;
using System.Windows;
using Microsoft.LightSwitch.Threading;
using HtmlAgilityPack;
namespace LightSwitchApplication
{
public partial class Table1ItemsListDetail
{
partial void Method_CanExecute(ref bool result)
{
// Write your code here.
}
partial void Method_Execute()
{
var wc = new HtmlWeb();
// string url = "http://www.nu.nl/feeds/rss/algemeen.rss";
wc.LoadAsync("http://google.com");
wc.LoadCompleted += new EventHandler<HtmlDocumentLoadCompleted>(DownloadCompleted);
//wc.
//
}
void DownloadCompleted(object sender, HtmlDocumentLoadCompleted e)
{
if (e.Error == null)
{
HtmlDocument doc = e.Document;
if (doc != null)
{
Dispatchers.Main.BeginInvoke(() =>
{
MessageBox.Show(doc.DocumentNode.Element("html").InnerHtml);
});
}
}
}
}
}

Related

What reference should I use for 'Data.ValueRange'?

I'm following a guide to write output data from Visual Studio into a google spreadsheet.
At the end of the guide there is a code block that I pasted inside my project:
using OpenQA.Selenium.Support.UI;
using System;
using NUnit.Framework;
using OpenQA.Selenium;
using System.Collections;
using System.Collections.Generic;
using Google.Apis.Sheets.v4;
using Google.Apis.Auth.OAuth2;
using System.IO;
using Google.Apis.Services;
using Newtonsoft.Json;
using WikipediaTests.Foundation_Class;
using System.Web;
using System.Data;
using Google.Apis.Sheets.v4.Data;
namespace AutomationProjects
{
[TestFixture]
public class TestClass : TestFoundation
{
public class SpreadSheetConnector
{
//Codeblock from guide pasted here!
}
[Test]
public void test1()
{
//Test case 1. Do XYZ...
}
}
}
In the code block included in the guide there is a section about creating a list and passing data into it:
// Pass in your data as a list of a list (2-D lists are equivalent to the 2-D spreadsheet structure)
public string UpdateData(List<IList<object>> data)
{
String range = "My Tab Name!A1:Y";
string valueInputOption = "USER_ENTERED";
// The new values to apply to the spreadsheet.
List<Data.ValueRange> updateData = new List<Data.ValueRange>();
var dataValueRange = new Data.ValueRange();
dataValueRange.Range = range;
dataValueRange.Values = data;
updateData.Add(dataValueRange);
Data.BatchUpdateValuesRequest requestBody = new Data.BatchUpdateValuesRequest();
requestBody.ValueInputOption = valueInputOption;
requestBody.Data = updateData;
var request = _sheetsService.Spreadsheets.Values.BatchUpdate(requestBody, _spreadsheetId);
Data.BatchUpdateValuesResponse response = request.Execute();
// Data.BatchUpdateValuesResponse response = await request.ExecuteAsync(); // For async
return JsonConvert.SerializeObject(response);
}
The problem is that I get an error for the 'Data.ValueRange' and the 'Data.BatchUpdateValuesRequest' :
CS0246 The type or namespace name 'Data' could not be found (are you missing a using directive or an assembly reference?)
I tried adding "System.Data" as a assembly reference to my project and then added it at the top (using). But it did not remove the error.
'Data.' seems to belong to "Google.Apis.Sheets.v4" but I have already added that reference as the guide instructed.
The only fix that gets rid of the error is adding Google.Apis.Sheets.v4 before every 'Data.' like this:
List<Google.Apis.Sheets.v4.Data.ValueRange>
But when I run my tests the output does not get exported to my spreadsheet. So I'm assuming this is not the correct solution. And also I'm assuming that the guide should have included this in the code block if it was necessary.
Could there be some other reference about 'Data' I need?
According to the documentation, the ValueRange Class depends of Sheets.v4.Data, so you should add:
using Google.Apis.Sheets.v4.Data;
Also, change:
List<Data.ValueRange> updateData = new List<Data.ValueRange>();
to:
List<ValueRange> updateData = new List<ValueRange>();

Screen Capture in C# using HtmlAgilityPack

Due to the lack of proper documentation, I'm not sure if HtmlAgilityPack supports screen capture in C# after it loads the html contents.
So is there a way I can more or less grab a screenshot using (or along with) HtmlAgilityPack so I can have a visual clue as to what happens every time I do page manipulations?
Here is my working code so far:
using HtmlAgilityPack;
using System;
namespace ConsoleApplication4
{
class Program
{
static void Main(string[] args)
{
string urlDemo = "https://htmlagilitypack.codeplex.com/";
HtmlWeb getHtmlWeb = new HtmlWeb();
var doc = getHtmlWeb.Load(urlDemo);
var sentence = doc.DocumentNode.SelectNodes("//p");
int counter = 1;
try
{
foreach (var p in sentence)
{
Console.WriteLine(counter + ". " + p.InnerText);
counter++;
}
}
catch (Exception e)
{
Console.WriteLine(e);
}
Console.ReadLine();
}
}
}
Currently, it scrapes and output all the p of the page in the console but at the same time I want to get a screen grab of the scraped contents but I don't know how and where to begin.
Any help is greatly appreciated. TIA
You can't do this with HTML Agility Pack. Use a different tool such as Selenium WebDriver. Here is how to do it: Take a screenshot with Selenium WebDriver
Could you use Selenium WebDriver instead?
You'll need to add the following NuGet packages to your project first:
Selenium.WebDriver
Selenium.Support
Loading a page and taking a screenshot is then as simple as...
using System;
using System.Drawing.Imaging;
using System.IO;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Support.UI;
namespace SeleniumTest
{
class Program
{
static void Main(string[] args)
{
// Create a web driver that used Firefox
var driver = new FirefoxDriver(
new FirefoxBinary(), new FirefoxProfile(), TimeSpan.FromSeconds(120));
// Load your page
driver.Navigate().GoToUrl("http://google.com");
// Wait until the page has actually loaded
var wait = new WebDriverWait(driver, new TimeSpan(0, 0, 10));
wait.Until(d => d.Title.Contains("Google"));
// Take a screenshot, and saves it to a file (you must have full access rights to the save location).
var myDesktop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
((ITakesScreenshot)driver).GetScreenshot().SaveAsFile(Path.Combine(myDesktop, "google-screenshot.png"), ImageFormat.Png);
driver.Close();
}
}
}

"The name does not exist in the current context"

I get the following error;
The name 'Request' does not exist in the current context
using System;
using System.Web;
using System.Web.UI;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using Microsoft.Exchange.WebServices.Data;
namespace Exchange101
{
// This sample is for demonstration purposes only. Before you run this sample, make sure that the code meets the coding requirements of your organization.
class Ex15_CreateMeetingOnBehalfOfPrinciple_CS
{
static ExchangeService service = Service.ConnectToService(UserDataFromConsole.GetUserData(), new TraceListener());
protected void Page_Load(object sender, EventArgs e)
{
var request = HttpContext.Current.Request.QueryString["source"];
HttpRequest q = Request;
NameValueCollection n = q.QueryString;
if (n.HasKeys())
{
string k = n.GetKey(0);
if (k == "one")
{
string v = n.Get(0);
}
if (k == "two")
{
string v = n.Get(0);
}
}
}
I'm an absolute newbie and have researched the error but am confused as to which assembly I might be missing as a reference.
issue may be here
var request = HttpContext.Current.Request.QueryString["source"];
HttpRequest q = Request;
your variable name is request bt you are using Request
change this as
var request = HttpContext.Current.Request.QueryString["source"];
HttpRequest q = request;
this wil solve your issue
Change this line:
class Ex15_CreateMeetingOnBehalfOfPrinciple_CS
to this:
class Ex15_CreateMeetingOnBehalfOfPrinciple_CS : System.Web.UI.Page
It looks like the problems you're getting are from properties you should be inheriting from that class.
If you are meaning HttpWebRequest, you should include the namespace
using System.Net;

LinkFinder.find fails to work in webcrawler app

Wrote code as the start to a web crawler that scrapes links from webpage.
Following the instructions from this page:
http://www.dotnetperls.com/scraping-html
I seem to get an error that LinkFinder cannot be found?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Diagnostics;
namespace WebCrawler
{
class Program
{
static void Main(string[] args)
{
WebClient url = new WebClient();
String initialLink = url.DownloadString("http://www.FAKEADDRESS.org.uk/");
for (LinkItem i in LinkFinder.find(initialLink))
{
System.Diagnostics.Debug.WriteLine(initialLink);
}
}
}
}
LinkFinder is a class that is included in the code at that URL you provided. Make sure you also copy that class into your project in some way (a file by itself, in another file, whatever).

C# Web Reference and PHP

I am attempting to call a Web Service (created in PHP) from my C# application. I have successfully added the Web Reference in Visual Studios, however I cannot figure out exactly how to invoke the call to the web service. I have been following this tutorial: http://sanity-free.org/article25.html however when I try and compile I get a "The type or namespace name 'SimpleService' could not be found". My C# code is as follows:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace inVision_OCR
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void translateButton_Click(object sender, EventArgs e)
{
// We need to snap the image here somehow . . .
// Open up the image and read its contents into a string
FileStream file = new FileStream("\\Hard Disk\\ocr_images\\test.jpg", FileMode.Open);
StreamReader sr = new StreamReader(file);
string s = sr.ReadToEnd();
sr.Close();
// Using SOAP, pass this message to our development server
SimpleService svc = new SimpleService();
string s1 = svc.getOCR("test");
MessageBox.Show(s);
}
}
}
The SimpleService is probably in a different namespace, look at the properties of the web reference you added and see what namespace the proxy class SimpleService is being generated in, then add a using statement in your code to reference that namespace.

Categories

Resources