【问题标题】:Zipcode to city/state look-up XML file?邮政编码到城市/州查找 XML 文件?
【发布时间】:2010-09-30 16:54:09
【问题描述】:

试图找到一个我可以用来代替查找数据库表的 XML 文件,直到我们将我们的网络托管切换到正确的数据库。

任何人都可以向我推荐一个 XML 文件,其中包含其子元素具有邮政编码、州和城市的元素吗?例如:

<zip code="98117">
    <state>WA</state>
    <city>Seattle</state>
</zip>

或者

<entry>
    <zip>98117</zip>
    <state>WA</state>
    <city>Seattle</city>
</entry>

我将在 C# 中使用 LINQ 来查询这些数据。

【问题讨论】:

    标签: linq zipcode


    【解决方案1】:
    【解决方案2】:

    有一个免费的邮政编码数据库位于:

    http://www.populardata.com

    我相信它是一个 .CSV 文件,但您可以很容易地将它转换为 XML 文件。

    【讨论】:

      【解决方案3】:

      这是根据输入的邮政编码执行 city.state 自动填充的代码。

      <script type="text/javascript">//<![CDATA[
      $(function() {
          // IMPORTANT: Fill in your client key
          var clientKey = "js-9qZHzu2Flc59Eq5rx10JdKERovBlJp3TQ3ApyC4TOa3tA8U7aVRnFwf41RpLgtE7";
      
          var cache = {};
          var container = $("#example1");
          var errorDiv = container.find("div.text-error");
      
          /** Handle successful response */
          function handleResp(data)
          {
              // Check for error
              if (data.error_msg)
                  errorDiv.text(data.error_msg);
              else if ("city" in data)
              {
                  // Set city and state
                  container.find("input[name='city']").val(data.city);
                  container.find("input[name='state']").val(data.state);
              }
          }
      
          // Set up event handlers
          container.find("input[name='zipcode']").on("keyup change", function() {
              // Get zip code
              var zipcode = $(this).val().substring(0, 5);
              if (zipcode.length == 5 && /^[0-9]+$/.test(zipcode))
              {
                  // Clear error
                  errorDiv.empty();
      
                  // Check cache
                  if (zipcode in cache)
                  {
                      handleResp(cache[zipcode]);
                  }
                  else
                  {
                      // Build url
                      var url = "http://www.zipcodeapi.com/rest/"+clientKey+"/info.json/" + zipcode + "/radians";
      
                      // Make AJAX request
                      $.ajax({
                          "url": url,
                          "dataType": "json"
                      }).done(function(data) {
                          handleResp(data);
      
                          // Store in cache
                          cache[zipcode] = data;
                      }).fail(function(data) {
                          if (data.responseText && (json = $.parseJSON(data.responseText)))
                          {
                              // Store in cache
                              cache[zipcode] = json;
      
                              // Check for error
                              if (json.error_msg)
                                  errorDiv.text(json.error_msg);
                          }
                          else
                              errorDiv.text('Request failed.');
                      });
                  }
              }
          }).trigger("change");
      });
      

      //]]>

      这里是 API - http://www.zipcodeapi.com/Examples#example1

      【讨论】:

        【解决方案4】:

        您可以通过以下方式请求 XML 中的内容。要直接以 XML 格式获取数据,您可以使用请求中格式的 .xml。

        https://www.zipcodeapi.com/rest/RbdapNcxbjoCvfCv4N5iwB3L4beZg017bfiB2u9eOxQkMtQQgV9NxdyCoNCENDMZ/info.xml/90210/degrees

        会回复

        <response>
           <zip_code>90210</zip_code>
           <lat>34.100501</lat>
           <lng>-118.414908</lng>
           <city>Beverly Hills</city>
           <state>CA</state>
           <timezone>
           <timezone_identifier>America/Los_Angeles</timezone_identifier>
           <timezone_abbr>PDT</timezone_abbr>
              <utc_offset_sec>-25200</utc_offset_sec>
              <is_dst>T</is_dst>
           </timezone>
           <acceptable_city_names/>
        </response>
        

        API 文档位于 https://www.zipcodeapi.com/API

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-04-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-01-27
          • 1970-01-01
          • 2015-09-25
          相关资源
          最近更新 更多