如何在网页上查找XML

时间:2015-07-03 23:16:12

标签: xml screen-scraping webpage

对于以下网页: http://www.realclearpolitics.com/epolls/2012/president/us/general_election_romney_vs_obama-1171.html

我如何找到与图表中的数据相关联的XML。我知道XML页面是http://charts.realclearpolitics.com/charts/1171.xml,因为有人告诉我。但是我怎么能自己解决这个问题?

由于

3 个答案:

答案 0 :(得分:0)

通常您可以在网站上找到rss链接或查看页面源并搜索.xml

您还可以在google上使用以下查询:

site:realclearpolitics.com filetype:xml

答案 1 :(得分:0)

您是否尝试过以下linq xml?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;


namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load("http://charts.realclearpolitics.com/charts/1171.xml");

        }
    }
}
​

答案 2 :(得分:0)

我终于得到了它的情节

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
using System.Xml;
using System.Xml.Linq;
using System.IO;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            XDocument doc = XDocument.Load("http://charts.realclearpolitics.com/charts/1171.xml");
            var results = new {
                X = doc.Descendants("series").Descendants("value").Select(y => (DateTime)y).ToList(),
                Obama = doc.Descendants("graph").Where(y => y.Attribute("title").Value == "Obama").Descendants("value").Select(y => (string)y == "" ?null : (double?)y).ToList(),
                Romney = doc.Descendants("graph").Where(y => y.Attribute("title").Value == "Romney").Descendants("value").Select(y => (string)y == "" ?null : (double?)y).ToList()
            };


            chart1.Series.Add("Obama");
            chart1.Series["Obama"].ChartType = SeriesChartType.Point;
            chart1.Series["Obama"].Points.DataBindXY(results.X, results.Obama);
             chart1.Series.Add("Romney");
            chart1.Series["Romney"].ChartType = SeriesChartType.Point;
            chart1.Series["Romney"].Points.DataBindXY(results.X, results.Romney);
            chart1.DataBind();

        }
    }
}
​