I am trying to create a simple Silverlight application that calls an ATOM feed and displays the article title and submit date. I found this very easy to do with RSS feeds and LINQ but I am stuck trying to do the same with an ATOM feed. The code below produces no errors but it also produced no results! What am I missing?
Source ATOM feed: weblogs.asp.net/scottgu/atom.aspx
Source Tutorial: www.switchonthecode.com/tutorials/silverlight-datagrid-the-basics
Source code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Xml.Linq;
namespace BasicDataGridTutorial
{
public partial class Page : UserControl
{
public Page()
{
InitializeComponent();
}
private void btnPopulate_Click(object sender, RoutedEventArgs e)
{
//disable the populate button so it's not clicked twice
//while the data is being requested
this.btnPopulate.IsEnabled = false;
//make a new WebClient object
WebClient client = new WebClient();
//hook the event that's called when the data is received
client.DownloadStringCompleted += client_DownloadStringCompleted;
//tell the WebClient to download the data asynchronously
client.DownloadStringAsync(
//new Uri("http://feeds.feedburner.com/SwitchOnTheCode?format=xml"));
new Uri("http://weblogs.asp.net/scottgu/atom.aspx"));
}
private void client_DownloadStringCompleted(object sender,
DownloadStringCompletedEventArgs e)
{
this.btnPopulate.IsEnabled = true;
if (e.Error == null)
{
XDocument document = XDocument.Parse(e.Result);
XNamespace xmlns = "http://www.w3.org/2005/Atom";
var sotcPosts = from entry in document.Descendants(xmlns+ "entry")
select new SOTCPost
{
Title = (string)entry.Element(xmlns + "feedEntryContent").Value,
Date = (string)entry.Element(xmlns + "lastUpdated").Value
};
this.sotcDataGrid.ItemsSource = sotcPosts;
}
}
private void btnClear_Click(object sender, RoutedEventArgs e)
{
this.sotcDataGrid.ItemsSource = null;
}
}
public class SOTCPost
{
public string Title { get; set; }
public string Date { get; set; }
}
}
I'd recommend using the SyndicationFeed instead of parsing the ATOM feed yourself. It'll do a better job of handling edge cases you may not have considered.
XmlReader reader = XmlReader.Create("http://localhost/feeds/serializedFeed.xml");
SyndicationFeed feed = SyndicationFeed.Load(reader);
var sotcPosts = from item in feed.Items
select new SOTCPost
{
Title = item.Title.Text,
Date = item.PublishDate
};
You have "feedEntryContent" and "lastUpdated" as element names, but I think you want "title" and "published".
The reason you get "no results" is that elements by the names you're selecting don't exist in the document.
Related
I converted a window of mine to use CefSharp in order to render some PDF file. One of the features the old component supported (WebBrowser of WPF, which unfortunately depends on IE, which does not work in this case) is accessing javascript elements through code in order to be aware of when the PDF has been scrolled to the bottom. Unfortunately this does not appear to work with CefSharp with the following code. Perhaps i am missing some initializer flag for this to work?
Currently in my Logs All i get is ScrollProgress|0|0|734
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Forms;
using System.Windows.Forms.Integration;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using CefSharp;
using NLog;
using Application = System.Windows.Application;
using MessageBox = System.Windows.MessageBox;
using WebBrowser = System.Windows.Forms.WebBrowser;
namespace MyNamespace
{
public partial class PdfConfirmWindow : Window
{
private static readonly ILogger Log = LogManager.GetLogger(nameof(PdfConfirmWindow));
public PdfConfirmWindow()
{
InitializeComponent();
}
private void WindowLoaded(object sender, RoutedEventArgs e)
{
ChromiumWebBrowser.LoadingStateChanged += ChromiumWebBrowserOnLoadingStateChanged;
UpdateBrowserContent();
}
private void ChromiumWebBrowserOnLoadingStateChanged(object sender, LoadingStateChangedEventArgs e)
{
if (!e.IsLoading)
{
Log.Debug("Loading completed");
Application.Current.Dispatcher.BeginInvoke(new Action(() =>
{
// (window.innerHeight + window.scrollY) >= document.body.offsetHeight
// e.scrollHeight - e.scrollTop === e.clientHeight
// CefSharp.PostMessage('EndOfDocumentReached');
const string script = #"
(function(){
var intervalHandler;
function onScrollCallback() {
var e = document.body;
var values = ['ScrollProgress'];
values.push(document.body.clientHeight);
values.push(window.scrollY);
values.push(window.innerHeight);
CefSharp.PostMessage(values.join('|'));
if ((window.innerHeight + window.scrollY) >= document.body.offsetHeight){
//clearInterval(intervalHandler);
}
}
setTimeout(function(){
var intervalHandler = setInterval(function(){
onScrollCallback();
}, 100);
}, 500);
})();
";
ChromiumWebBrowser.Focus();
ChromiumWebBrowser.WebBrowser.JavascriptMessageReceived += WebBrowserOnJavascriptMessageReceived;
ChromiumWebBrowser.WebBrowser.ExecuteScriptAsync(script);
}));
}
}
private async void WebBrowserOnJavascriptMessageReceived(object sender, JavascriptMessageReceivedEventArgs e)
{
await UIHelper.Thread;
// ChromiumWebBrowser.WebBrowser.JavascriptMessageReceived -= WebBrowserOnJavascriptMessageReceived;
if (e.Message?.ToString().Equals("EndOfDocumentReached", StringComparison.OrdinalIgnoreCase) == true)
{
SetReachedStatus(true);
}
if (e.Message?.ToString().StartsWith("ScrollProgress", StringComparison.OrdinalIgnoreCase) == true)
{
Log.Debug(e.Message?.ToString());
}
}
}
why can't I add a new item/object to my list in button (or combobox etc.) events? I mean, the events don't see my list if it's outside of the brackets...it's underlined in red... can you help me?
long story short: i want to add a new object by clicking the button
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Serialization;
using System.IO;
namespace Samochody
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
List<Samochod> ListaSamochodow = new List<Samochod>();
comboBox1.DataSource = ListaSamochodow;
comboBox1.DisplayMember = "Marka";
XmlRootAttribute oRootAttr = new XmlRootAttribute();
XmlSerializer oSerializer = new XmlSerializer(typeof(List<Samochod>), oRootAttr);
StreamWriter oStreamWriter = null;
oStreamWriter = new StreamWriter("samochody.xml");
oSerializer.Serialize(oStreamWriter, ListaSamochodow);
}
private void button1_Click(object sender, EventArgs e)
{
try
{
ListaSamochodow.Add(new Samochod(textBox1.Text, textBox2.Text, Convert.ToInt32(textBox3.Text)));
}
catch (Exception oException)
{
Console.WriteLine("Aplikacja wygenerowała następujący wyjątek: " + oException.Message);
}
}
I think you should instantiate your list globally and not under Form1_Load event.
that way it will be accessible all around your class (the window in this case).
This seems to work fine. You might need to set your textboxes to contain valid values when you start the form. also make sure that you make the list visible throughout your form1 class.
namespace Samochody
{
public partial class Form1 : Form
{
// make sure your list looks like this, created outside your functions.
List<Samochod> ListaSamochodow = new List<Samochod>();
public Form1()
{
InitializeComponent();
label1.Text = "the amount in your list is " + ListaSamochodow.Count.ToString();
textBox1.Text = "string here";
textBox2.Text = "string here";
textBox3.Text = "100";
}
private void Form1_Load(object sender, EventArgs e)
{
XmlRootAttribute oRootAttr = new XmlRootAttribute();
XmlSerializer oSerializer = new XmlSerializer(typeof(List<Samochod>), oRootAttr);
StreamWriter oStreamWriter = null;
oStreamWriter = new StreamWriter("samochody.xml");
oSerializer.Serialize(oStreamWriter, ListaSamochodow);
}
private void button1_Click_1(object sender, EventArgs e)
{
Samochod s = new Samochod(textBox1.Text, textBox2.Text, Convert.ToInt32(textBox3.Text));
ListaSamochodow.Add(s);
label1.Text = "the amount in your list is " + ListaSamochodow.Count.ToString();
}
}
}
In this Html code I need to get attributes and data from html tags. This is an example:
...
<tr class="first-row"><td class="first-cell tl">Gefle - Kalmar</td><td class="result">4:2</td><td class="odds best-betrate" data-odd="3.53"></td><td class="odds" data-odd="3.37"></td><td class="odds" data-odd="2.04"></td><td class="last-cell nobr date">18.07.2016</td></tr>
...
So, I need to get data between td tags and attributes (data-odd).
This is my C# code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using HtmlAgilityPack;
namespace bexscraping
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
string url = "http://www.betexplorer.com/soccer/sweden/allsvenskan/results/";
HtmlWeb web = new HtmlWeb();
HtmlAgilityPack.HtmlDocument doc = web.Load(url);
foreach (HtmlNode node in doc.DocumentNode.SelectNodes("//table"))
{
//node.Remove();
outputLabel.Text += node.InnerText;
}
}
}
}
Any suggestion? Thanks!
Here it is some Msdn Examples: XPath Examples; XPath Reference
According to your snippet you could select all tags TD that contain attribute data-odd.
private void Form1_Load(object sender, EventArgs e)
{
string url = "http://www.betexplorer.com/soccer/sweden/allsvenskan/results/";
HtmlWeb web = new HtmlWeb();
HtmlAgilityPack.HtmlDocument doc = web.Load(url);
var nodes = doc.DocumentNode.SelectNodes("//td[#data-odd]");
foreach (HtmlNode node in nodes)
{
//Here process your node
//Example: to get data-odd value
var val = node.GetAttributeValue("data-odd", "");
}
}
I'm trying to scrape a website to get data off of it. So far I got it to at least connect to the website, but now when I try to set the text of a textbox with the data, I just get a bunch of:
HtmlAgilityPack.HtmlNodeCollection
There are the same number of HtmlAgilityPack.HtmlNodeCollection as there is data. Here is my code ( it is a bit sloppy I know ):
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using System;
using HtmlAgilityPack;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
string choice;
public Form1()
{
InitializeComponent();
}
public void comboBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
}
public void button1_Click(object sender, System.EventArgs e)
{
HtmlAgilityPack.HtmlDocument htmlDoc = new HtmlAgilityPack.HtmlDocument();
htmlDoc.OptionFixNestedTags = true;
string urlToLoad = "http://www.nbcwashington.com/weather/school-closings/";
HttpWebRequest request = HttpWebRequest.Create(urlToLoad) as HttpWebRequest;
request.Method = "GET";
Console.WriteLine(request.RequestUri.AbsoluteUri);
WebResponse response = request.GetResponse();
htmlDoc.Load(response.GetResponseStream(), true);
if (htmlDoc.DocumentNode != null)
{
var articleNodes = htmlDoc.DocumentNode.SelectNodes("/html/body/div/div/div/div/div/div/p");
if (articleNodes != null && articleNodes.Any())
{
foreach (var articleNode in articleNodes)
{
textBox1.AppendText(htmlDoc.DocumentNode.SelectNodes("/html/body/div/div/div/div/div/div/p").ToString());
}
}
}
Console.ReadLine();
}
private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
choice = listBox1.SelectedItem.ToString();
}
}
}
So what am I missing / doing wrong here? The data should return something like:
Warren County Public Schools Closed
Washington Adventist University Closing at Noon
Thanks for looking at this.
Nevermind, found the issue. I guess I was trying to grab the document node instead of the inner text... Here is code just in case anyone wants it.
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using System;
using HtmlAgilityPack;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
string choice;
public Form1()
{
InitializeComponent();
}
public void comboBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
}
public void button1_Click(object sender, System.EventArgs e)
{
HtmlAgilityPack.HtmlDocument htmlDoc = new HtmlAgilityPack.HtmlDocument();
htmlDoc.OptionFixNestedTags = true;
string urlToLoad = "http://www.nbcwashington.com/weather/school-closings/";
HttpWebRequest request = HttpWebRequest.Create(urlToLoad) as HttpWebRequest;
request.Method = "GET";
Console.WriteLine(request.RequestUri.AbsoluteUri);
WebResponse response = request.GetResponse();
htmlDoc.Load(response.GetResponseStream(), true);
if (htmlDoc.DocumentNode != null)
{
var articleNodes = htmlDoc.DocumentNode.SelectNodes("/html/body/div/div/div/div/div/div/p");
if (articleNodes != null && articleNodes.Any())
{
int k = 0;
foreach (var articleNode in articleNodes)
{
textBox1.AppendText(articleNode.InnerText + "\n");
}
}
}
Console.ReadLine();
}
private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
choice = listBox1.SelectedItem.ToString();
}
}
}
Since articleNodes already contain the nodes you're interested in, there's no need to call SelectNodes() again inside a loop.
Also, you shouldn't need to check for null, since articleNodes is a collection. It might be empty, but shouldn't be null.
Try this, accessing the InnerHtml (or InnerText) property instead:
var articleNodes = htmlDoc.DocumentNode.SelectNodes("/html/body/div/div/div/div/div/div/p");
var result = articleNodes.Select(x => x.InnerHtml.Replace("<br><span>", " ")
.Replace(" </span>", "")).ToList();
I am trying to write an editor for an xml config file. I have the xml file bound to a listview and selecting an element and then clicking edit allows you to edit an element. When the user clicks save, a delegate is called which updates the element within the xml document. I have tried to do the updating in various ways including appendnode, prependnode, insertafter, and replace child. I have gotten them all working without any compile time or runtime errors, yet they don't update the xmldataprovider or the listview. It's probably just something simple that I am missing but I am having trouble figuring out what it is. Can someone please help?
Thanks,
Brian
If it helps, here's the source code of my main form (the one with the listview)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xml;
using System.IO;
namespace g2config
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public XmlDataProvider dp;
string cfgfile;
public managecmpnts manage_components;
public delegate void managecmpnts(int id, XmlElement component);
public MainWindow()
{
InitializeComponent();
dp = this.FindResource("configfile") as XmlDataProvider;
cfgfile = "C:\\Users\\briansvgs\\Desktop\\g2config\\g2config\\bin\\Debug\\g2config.pxml";
if(Environment.GetCommandLineArgs().Count() > 1)
{
string path = Environment.GetCommandLineArgs().ElementAt(1);
//MessageBox.Show("Path: " + path);
cfgfile = path;
}
if (File.Exists(cfgfile))
{
dp.Source = new Uri(cfgfile, UriKind.RelativeOrAbsolute);
}
else
{
MessageBox.Show("config file not found");
}
this.Title = this.Title + " (" + cfgfile + ") ";
}
public void browsedir( object sender, EventArgs e)
{
System.Windows.Forms.OpenFileDialog ofd = new System.Windows.Forms.OpenFileDialog();
ofd.Filter = "PXML Files (*.pxml)|*.pxml"; ;
//http://www.kirupa.com/net/using_open_file_dialog_pg4.htm
if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK) //how to browse dirs instead of files?????
{
startpath.Text = ofd.FileName;
}
}
public void newcomponent(object sender, RoutedEventArgs e)
{
componentsettings new_cmpnt = new componentsettings();
new_cmpnt.Title = "Add Component";
new_cmpnt.ShowDialog();
}
public void editcomponent(object sender, RoutedEventArgs e)
{
int selected = components.SelectedIndex;
XmlElement current = (XmlElement)components.Items.CurrentItem;
componentsettings edit_cmpnt = new componentsettings(current);
edit_cmpnt.cmpnt_mgr.manage_components += new manager.mngcmpnts(test);
edit_cmpnt.Title = "Edit Component";
edit_cmpnt.ShowDialog();
}
private void browsedir(object sender, RoutedEventArgs e)
{
System.Windows.Forms.FolderBrowserDialog directory;
directory = new System.Windows.Forms.FolderBrowserDialog();
directory.Description = "Please select the folder containing your g2 configuration (pxml) files. Default is C:\\Program Files\\Exacom, Inc.";
directory.ShowDialog();
startpath.Text = directory.SelectedPath;
}
private void save(object sender, RoutedEventArgs e)
{
MessageBox.Show(dp.Source.ToString());
if(File.Exists(cfgfile + ".old"))
{
File.Delete(cfgfile + ".old");
}
File.Move(cfgfile, cfgfile + ".old");
dp.Document.Save(cfgfile);
}
private void remove(object sender, RoutedEventArgs e)
{
XmlNode component = dp.Document.DocumentElement["Components"].ChildNodes[components.SelectedIndex];
dp.Document.DocumentElement["Components"].RemoveChild(component);
}
private void temp(object sender, RoutedEventArgs e)
{
MessageBox.Show(dp.Document.DocumentElement["Components"].ChildNodes[components.SelectedIndex].InnerText.ToString()); //will this give me trouble????? Probably not since it's my config, but...
}
private void test(int id, XmlElement testelement)
{
//dp.Document.DocumentElement["Components"].ChildNodes[id] = testelement;
//XmlNode testnode = dp.Document.DocumentElement["Components"].ChildNodes[id + 1];
//XmlNode testnode = testelement;
MessageBox.Show(testelement.OuterXml.ToString());
//dp.Document.DocumentElement["Components"].InsertAfter(dp.Document.DocumentElement["Components"].ChildNodes[0], testelement);
//dp.Document.DocumentElement["Components"].RemoveChild(testelement);
//dp.Document.DocumentElement["Components"].ReplaceChild(testnode, dp.Document.DocumentElement["Components"].ChildNodes[id]);
dp.Document.DocumentElement["Components"].PrependChild(testelement);
//dp.Document.NodeChanged();
//dp.Document.DocumentElement["Components"].CloneNode(true);
dp.Document.DocumentElement["Components"].AppendChild(testelement);
//dp.Document.
//MessageBox.Show(dp.Document.OuterXml.ToString());
//MessageBox.Show(dp.Document.DocumentElement["Components"].FirstChild.AppendChild(testelement).OuterXml.ToString());
}
}
}
JMSA. Thank you for the suggestion. I was playing around with it today and I found a solution. It's a little messy, but it works. I copy one of the present nodes and then clear all of the values. Here is the code if anyone is interested.
private void add(object sender, RoutedEventArgs e)
{
System.Xml.XmlNode test = configfile.Document.DocumentElement["Components"].FirstChild;
System.Xml.XmlNode testclone = test.Clone();
for (int i = 0; i < testclone.ChildNodes.Count; i++)
{
testclone.ChildNodes[i].RemoveAll();
}
configfile.Document.DocumentElement["Components"].AppendChild(testclone);
components.SelectedItem = components.Items.Count + 1;
}
My suggestion would be to create a class for each element by using a composite pattern, lazy loading the XML doc in the class and perform operations as you need. Then again save it. Quite a handful though.