I'm having trouble coming up with the correct code to download pictures from a RSS feed and then handing that download off to RadControls slide View or Pagination.
The only thing I can get with the code I'm using is either the text for the pictures or just thumbnails of the pictures, not the full image. I must be missing something or leaving something out.
This is for Windows Phone C#
The web links for the RSS feeds are generic for testing purposes.
//Constructor
public MainPage()
{
InitializeComponent();
}
private void FlickrSearch_Click(object sender, RoutedEventArgs e)
{
WebClient webclient = new WebClient();
webclient.DownloadStringCompleted += new DownloadStringCompletedEventHandler
(webclient_DownloadStringCompleted);
}
void webclient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
if (e.Error != null)
{
MessageBox.Show("error");
}
// parsing Flickr
XElement XmlTweet = XElement.Parse(e.Result);
XNamespace ns = "http://api.flickr.com/services/feeds/photos_public.gne?tag="; // flilckr
listBox1.ItemsSource =
from tweet in XmlTweet.Descendants("item")
select new FlickrData
{
ImageSource = tweet.Element(ns + "thumbnail").Attribute("url").Value,
Message = tweet.Element("description").Value,
UserName = tweet.Element("title").Value,
PubDate = DateTime.Parse(tweet.Element("pubDate").Value)
};
}
I have found the way you are parsing & fetching the inner data is erroneous. I have closely verified the XML data from Flickr and modified the logic accordingly.
Here goes the complete code:
void webclient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
if (e.Error != null)
{
MessageBox.Show("error");
}
// parsing Flickr
XElement XmlTweet = XElement.Parse(e.Result);
string ns = "http://www.w3.org/2005/Atom";
XName entry = XName.Get("entry", ns);
XName loc = XName.Get("loc", ns);
XName title = XName.Get("title", ns);
XName published = XName.Get("published", ns);
XName link = XName.Get("link", ns);
XName content = XName.Get("content", ns);
XName url = XName.Get("url", ns);
listBox1.ItemsSource =
from tweet in XmlTweet.Elements(entry)
select new FlickrData
{
ImageSource = tweet.Element(link).Attribute("href").Value,
Message = tweet.Element(content).Value,
UserName = tweet.Element(title).Value,
PubDate = DateTime.Parse(tweet.Element(published).Value)
};
}
Related
I have searched for solution for this but all I found are questions how to prevent this.
I have a simple code for saving and loading XML.
XDocument xDoc;
public Form1()
{
InitializeComponent();
xDoc = new XDocument(new XElement("root", new XElement("data")));
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
string input = textBox1.Text;
xDoc.Root.SetElementValue("data", input);
rtxtXPreview.Text = xDoc.ToString();
txtValuePreview.Text = xDoc.Root.Element("data").Value;
}
private void button1_Click(object sender, EventArgs e)
{
SaveAndLoad();
}
private void SaveAndLoad()
{
string dataNodeValue;
xDoc.Save(path);
dataNodeValue = xDoc.Descendants("data").First().Value;
txtValuePreview.Text = dataNodeValue;
rtxtXPreview.Text = xDoc.ToString();
}
textBox1: is the input for the data element.
rtxtXPreview: is a rich textbox for displaying the text of the XML.
txtValuePreview: is a textbox for displaying the element value.
The problem is that when I enter value like:
A
B
C
The XML shows fine with the line breaks but once I hit the button the document is saved without those. So when I debugged my code I found that the characters \n\r is replaced with \n so I searched for this problem and found this code:
private void SaveAndLoadFixed()
{
string dataNodeValue;
XmlWriterSettings xmlWriterSettings =
new XmlWriterSettings { NewLineHandling = NewLineHandling.None, Indent = true };
using (XmlWriter xmlWriter = XmlWriter.Create(path, xmlWriterSettings))
{
xDoc.Save(xmlWriter);
xmlWriter.Flush();
}
//xDoc.Save(path);
XElement xDatabaseElement;
using (XmlTextReader xmlTextReader = new XmlTextReader(path) { WhitespaceHandling = WhitespaceHandling.Significant })
{
xmlTextReader.MoveToContent();
xDatabaseElement = XElement.Load(xmlTextReader);
}
dataNodeValue = xDatabaseElement.Descendants("data").First().Value;
txtValuePreview.Text = dataNodeValue;
rtxtXPreview.Text = xDatabaseElement.ToString();
}
This worked fine but why the characters \n\r not replaced with  because when I tried to type the character < or > it was replaced with < & > respectively ?
When I write the HTML to a file, and open the file, it says "The inventory is not available" I tried using a user-agent, but I had no luck. Any help?
The result I was hoping for is to grab the entire inventory, and print out the list of a desired page on their inventory to the listbox. (Windows Forms Application, HtmlAgilityPack)
namespace projectWFA
{
public partial class Form1 : Form
{
public string selectedCurrency;
public string urlBase = "http://www.steamcommunity.com/id/";
public string text_username;
// HTMLAGILITYPACK
public HtmlWeb htmlweb;
public HtmlAgilityPack.HtmlDocument htmldocument_pageInventory;
public HtmlAgilityPack.HtmlDocument htmldocument;
public Form1()
{
InitializeComponent();
comboboxCurrency.SelectedIndex = 0;
comboboxPlaceHolder1.SelectedIndex = 0;
comboboxPlaceHolder2.SelectedIndex = 0;
// WEB STUFF
htmlweb = new HtmlWeb();
}
private void Form1_Load(object sender, EventArgs e)
{
selectedCurrency = comboboxCurrency.Items[comboboxCurrency.SelectedIndex].ToString();
Console.Out.WriteLine("selCur: " + selectedCurrency);
}
private void buttonAccept_Click(object sender, EventArgs e)
{
labelPrintUsername.Text = "Loading webpage.. Please wait..";
// LOAD WEBPAGE
htmldocument = htmlweb.Load(urlBase + textboxProfileLink.Text);
Console.Out.WriteLine("Link: " + urlBase + textboxProfileLink.Text);
if (htmldocument != null)
{
Console.Out.WriteLine("Found profile");
HtmlNode node_username = htmldocument.DocumentNode.SelectSingleNode("//span[#class='actual_persona_name']");
if (node_username != null)
{
text_username = node_username.InnerText;
labelPrintUsername.Text = text_username + "'s Inventory";
listboxItems.Items.Clear();
htmldocument_pageInventory = htmlweb.Load(urlBase + textboxProfileLink.Text + "/inventory/");
System.IO.File.WriteAllText(#"C:\Users\...\Desktop\asd.html", htmldocument_pageInventory.DocumentNode.InnerHtml);
}
}
else if (htmldocument == null)
{
Console.Out.WriteLine("Couldn't find profile");
}
}
}
}
To get the page as it would be from using a browser, use this instead:
string data = "";
using (WebClient client = new WebClient())
{
data = client.DownloadString(url);
}
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(data));
doc.Load(stream);
Trying to get an xpath and then display the contents of the inner text of that xpath.
private void htmlDocument_Click(object sender, HtmlElementEventArgs e)
{
HtmlElement element = this.wbrBrowser.Document.GetElementFromPoint(e.ClientMousePosition);
var savedId = element.Id;
var uniqueId = Guid.NewGuid().ToString();
element.Id = uniqueId;
var doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(element.Document.GetElementsByTagName("html")[0].OuterHtml);
element.Id = savedId;
var node = doc.GetElementbyId(uniqueId);
var xpath = node.XPath;
lblXpath.Text = xpath;
}
private void button1_Click(object sender, EventArgs e)
{
var w = new HtmlWeb();
var doc = w.Load(lstURL.Text);
var price = doc.DocumentNode.SelectSingleNode(lblXpath.Text);
if (price != null) lblPrice.Text = price.InnerHtml;
}
price is always null. Why is it that htmlagilitypack is not getting the element that it provided the xpath for?
I have urls in a listbox. I am trying to navigate to a url when it is selected.
private void lstURL_SelectedIndexChanged(object sender, EventArgs e)
{
wbrBrowser.Navigate(lstURL.Text);
lblUrl.Text = lstURL.Text;
lblTitle.Text = "Loading...";
System.Windows.Forms.HtmlDocument document = wbrBrowser.Document;
document.MouseUp += new HtmlElementEventHandler(this.htmlDocument_Click);
}
private void wbrBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
lblTitle.Text = wbrBrowser.Document.Title;
}
private void htmlDocument_Click(object sender, HtmlElementEventArgs e)
{
HtmlElement element = this.wbrBrowser.Document.GetElementFromPoint(e.ClientMousePosition);
var savedId = element.Id;
var uniqueId = Guid.NewGuid().ToString();
element.Id = uniqueId;
var doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(element.Document.GetElementsByTagName("html")[0].OuterHtml);
element.Id = savedId;
var node = doc.GetElementbyId(uniqueId);
var xpath = node.XPath;
lblXpath.Text = xpath;
}
It works the first time I load a page, after that it just freezes and lblTitle.Text just stays at "Loading..."
I have been searching for a while but I can't figure out why this is happening.
I want to get some data from a website but my code can't get url. Because if state is always false and my textbox called "weather1" always display "ece".
When I make the value of textbox as e.Result instead of "ece", it displays "DownloadStringCompletedEventArgs ".
And there is no error or warning.
What should I do? What's wrong with the code?
public void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
if (!e.Cancelled && e.Error == null)
{
string res = Convert.ToString(e.Result);
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(res);
var table = doc.DocumentNode.SelectSingleNode("//table[#class='tbl_sond']");
var degree = table.SelectSingleNode("//td[#class='renkMax']");
var date = table.SelectSingleNode("//td[#class='sond_zaman']");
}
else weather1.Text = "ece";
}
public void getWeatherInfo() {
string url = "http://www.mgm.gov.tr/tahmin/il-ve-ilceler.aspx?m=ISTANBUL";
WebClient webclient = new WebClient();
webclient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
Uri uri = new Uri(url);
webclient.DownloadStringAsync(uri);
}