Kobarin's Development Blog

C#やASP.NETなどについての記録です。

じゃらんAPIを使って、特定ホテルの空室を調べる(部屋情報を出力)

かなり手抜きだけど、ひとまずメモということで。後日修正版を書き直す。

  protected void btnSearch_Click(object sender, EventArgs e)
  {
    const string strUrlFormat = "http://jws.jalan.net/APIAdvance/StockSearch/V1/?key={0}&h_id={1}&stay_date={2:yyyyMMdd}&stay_count={3}";

    const string strApiKey = "APIキー";
    const string strHotelKey = "ホテルID";
    const int numStayCount = 1;
    string strStayDate = "20100129";    //検索したい日付
    string strUrl = String.Format(strUrlFormat, strApiKey, strHotelKey, strStayDate, numStayCount);
    linkUrl.NavigateUrl = strUrl;


    panelResult.Visible = true;
    lblResult.Text = "";

    XmlTextReader reader = null;
    try
    {
      reader = new XmlTextReader(strUrl);

      while (reader.Read())
      {
        if (reader.NodeType == XmlNodeType.Element)
        {
          string strValue = reader.ReadString();

          int numStock = -1;
          string strStock = "";

          switch (reader.LocalName)
          {
            case "RoomName"://部屋名
              lblResult.Text += String.Format("[{0}]
", strValue); break; case "RateType"://料金タイプ(大人1名あたり) lblResult.Text += String.Format("  {0}", strValue); break; case "Rate"://1室1泊あたりの料金 lblResult.Text += String.Format("{0}円", reader.ReadString()); break; case "Stock"://空き室数(10以上は空) if (!int.TryParse(strValue, out numStock)) numStock = 10; if (numStock >= 3) strStock = "○"; else if (numStock >= 1) strStock = "△"; else strStock = "×"; lblResult.Text += String.Format("・・・{0}
", strStock); break; default: break; } } } if (lblResult.Text == "") lblResult.Text = "満室です"; } catch (Exception ex) { } finally { reader.Close(); } }