Cristina Ursu
Cristina Ursu

Reputation: 201

How to use Html Agility Pack with windows 8 apps?

First of all I want to say that I am new to c# and Windows 8 apps. So, please, don't be hard on me.

I have the following code to extract some images urls and to save them in a XML file. I am using Html Agility Pack, but when I try to use the code with a Windows 8 application it doesn't work. I know that I have to use the Fizzler Html Agility Pack from here: http://fizzlerex.codeplex.com/releases/view/89833 but I don't know what is wrong. I am using visual studio 2012 and it does not recognize the following elements:

***WebClient*** x = new ***WebClient***();  
***XmlDocument*** output = new ***XmlDocument***();  
***XmlElement*** imgElements = output.CreateElement("ImgElements");  
foreach(HtmlNode link in document.***DocumentElement***.SelectNodes("//img[contains(@src, '_412s.jpg')]"));                                             
***out***.Save(@"C:\test.xml");

Code:

using HtmlAgilityPack;
using Fizzler;
using Fizzler.Systems.HtmlAgilityPack;
using System.Xml;

public void Images()
{
    WebClient x = new WebClient();
    string source = x.DownloadString(@"http://www.google.com");
    HtmlAgilityPack.HtmlDocument document = new HtmlAgilityPack.HtmlDocument();
    document.Load(source);
    XmlDocument output = new XmlDocument();
    XmlElement imgElements = output.CreateElement("ImgElements");
    output.AppendChild(imgElements);
    foreach(HtmlNode link in document.DocumentElement.SelectNodes("//img[contains(@src, '_412s.jpg')]"))
    {
        XmlElement img = output.CreateElement(link.Name);
        foreach(HtmlAttribute a in link.Attributes)
        {
            img.SetAttribute(a.Name, a.Value);
        }
        imgElements.AppendChild(img);
    }
    out.Save(@"C:\test.xml");
}

Can you please help me?

Thank you !

Upvotes: 0

Views: 1039

Answers (2)

Daniel San
Daniel San

Reputation: 2036

Try something like this:

HttpClientHandler handler = new HttpClientHandler();
HttpClient client = new HttpClient(handler as HttpMessageHandler) { BaseAddress = new Uri(@"http://www.google.com") };
var r = await client.GetAsync(client.BaseAddress);
string html;
if (r.IsSuccessStatusCode) html = await r.Content.ReadAsStringAsync();

Upvotes: 0

Steven Doggart
Steven Doggart

Reputation: 43743

out.Save(@"C:\test.xml");

Should be:

output.Save(@"C:\test.xml");

And then you need to add the following two namespaces to then top of the code file:

using System.Xml;
using System.Net;

These errors have nothing to do with Windows 8. They would be errors in any version. I'm not sure why you'd need to switch from the WebClient class to the HttpClient class, since they are both supported in Windows 8, but, if you want to use the HttpClient class, something like this should work:

HttpClient x = new HttpClient();
Task<string> t = x.GetStringAsync(@"http://www.google.com");
t.Wait();
string source = t.Result;

Upvotes: 1

Related Questions