错误''doctype'是一个意外的令牌。预期的令牌是'DOCTYPE'。第1行,第3位。'

时间:2014-04-20 09:39:40

标签: c# asp.net google-api

嘿大家我是新手使用google api获取天气更新。问题它给出了一个错误.. 我设置了doctype但问题没有解决..请提前帮助我...

.aspx文件

<%@ Page Language="C#"  CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication4.WebForm1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head id="Head1" runat="server">
    <title>Put Your Title Here</title>
    <style type="text/css">
        #Panel1
        {        
            width:350px;         
        }           
        #Panel1 img
        {
            float:left;
            width:100px;
            height:100px;
            margin-top:10px;
        }
        #Panel1 p
        {        
            float:right;
            margin-top:-10px;
            margin-right:0px;        
        }
        #Panel1 legend
        {        
            background-color:Green;
            color:White;
        }
    </style> 
</head>
<body>
    <form id="form1" runat="server">
        <h1 style="text-align:left">Using The Google Weather API</h1><br /> 
        <div style="text-align:left">
            <b>Enter Location (Postal Code or City): </b><asp:TextBox ID="txtLocation" Text="10007" runat="server"></asp:TextBox>
            <asp:Button ID="btnGo" runat="server" Text="GO" onclick="btnGo_Click" /><br /><br />

            <asp:Panel ID="Panel1" runat="server">
                <asp:Image ImageUrl="" runat="server" ID="icon" /> <br />
                <p>
                    Forecast for <b><asp:Label ID="lblLocation" runat="server"></asp:Label></b><br />
                    Current Condition: <b><asp:Label ID="currCondition" runat="server" Text=""></asp:Label></b><br />
                    <b><asp:Label ID="temp_f" runat="server" CssClass = "temp" Text=""></asp:Label></b>
                    <b><asp:Label ID="temp_c" runat="server" Text=""></asp:Label></b><br />
                    <b><asp:Label ID="humidity" runat="server" Text=""></asp:Label></b><br />
                    <b><asp:Label ID="wind_condition" runat="server" Text=""></asp:Label></b><br />
                    Forecast Date: <b><asp:Label ID="lblForecastDate" runat="server" Text=""></asp:Label></b><br />
                </p>
            </asp:Panel><br />
                <div>
                    <b><asp:Label id="wdcError" runat="server" Visible="false" CssClass="weatherError" ></asp:Label></b>
                </div>
        </div>
    </form>
<br/>       
</body>
</html>

.cs文件

  protected void btnGo_Click(object sender, EventArgs e)
        {
            // retrieve weather for the zip code entered by the user
            getWeatherData();
        }

        public void getWeatherData()
        {
            wdcError.Visible = false;

            // check for empty zip code field
            if (txtLocation.Text.Length == 0)
            {
                wdcError.Visible = true;
                wdcError.Text = "Please enter a location!";
                return;
            }
            // load xml result from Google weather
            XDocument xd = XDocument.Load("http://www.google.com/ig/api?weather=" + txtLocation.Text);

            // used to determine if a node is missing
            int cnt = 0;
            cnt = xd.Descendants("forecast_information").Count();

            // determine if forecast information was returned for the location entered by user
            if (cnt == 0)
            {
                wdcError.Visible = true;
                wdcError.Text = "Forecast Information NOT available for the location";
                return;
            }

            // navigate to the Current Conditions node
            var current_conditions = from currentCond in xd.Root.Descendants("current_conditions")
                                     select currentCond;

            // navigate to the Forecast Information node
            var forcastInfo = from forecastinfo in xd.Root.Descendants("forecast_information")
                              select forecastinfo;

            Panel1.GroupingText = "Today's Weather";

            // retrieve city and forecast date information
            foreach (var item in forcastInfo)
            {
                lblLocation.Text = item.Element("city").Attribute("data").Value;
                lblForecastDate.Text = item.Element("forecast_date").Attribute("data").Value;
            }

            // retrieve current weather conditions information
            foreach (var item in current_conditions)
            {
                currCondition.Text = item.Element("condition").Attribute("data").Value;
                temp_f.Text = item.Element("temp_f").Attribute("data").Value + "&deg;" + "F";
                temp_c.Text = "  (" + item.Element("temp_c").Attribute("data").Value + "&deg;" + "C" + ")";
                humidity.Text = item.Element("humidity").Attribute("data").Value;
                icon.ImageUrl = "http://www.google.com" + item.Element("icon").Attribute("data").Value;
                wind_condition.Text = item.Element("wind_condition").Attribute("data").Value;
            }
        }

2 个答案:

答案 0 :(得分:1)

这是因为您的http://www.google.com/ig/api?weather=" + txtLocation.Text没有返回xml文件。该链接只是将您重定向到谷歌页面。

谷歌在之前的某个时候杀死了天气预报。 http://thenextweb.com/google/2012/08/28/did-google-just-quietly-kill-private-weather-api/

微软的天气API非常类似于Google的

检查此示例
http://weather.service.msn.com/data.aspx?weadegreetype=F&culture=en-US&weasearchstr=Chicago,IL

或者您可以使用Wunderground api

答案 1 :(得分:1)

如果您最喜欢的天气应用程序在上周左右突然停止工作,那么尝试刷新或重新安装它是没有意义的。谷歌一言不发地关闭了它的天气API,并搁浅了依赖它来为他们的天气相关应用程序提供支持的开发人员。我们留下的是一堆破碎的应用程序,可能会或可能不会得到修复。

我的朋友尝试别的东西了

你可以从这里得到

http://www.yr.no/

http://www.weathercase.net/

MSN weather API list of conditions?

由于