Connecting to API Using C#.NET
Included here you will find simple sample code for parsing the Database Interface API in XML.
The first example returns primary data for a given jurisdiction - the result containing the
United States Post Office. Some ZIP codes include multiple cities and sometimes multiple tax jurisdictions,
so the second example shows how to go down the hierarchy and parse the XML.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="mssql-basic.cs" Inherits="CSharpBasic" %>
<!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">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Net;
using System.Xml;
using System.Xml.Linq;
public partial class CSharpBasic : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string zipCode = "90210"; //sample zip code must be between 90001 and 90999
string username = "sample";
string password = "password";
//string cmdText = "exec z2t_lookup '" + zipCode + "', '" + username + "', '" + password + "'";
string url = "https://api.zip2tax.com/TaxRate-USA.xml?username=" + username + "&password=" + password + "&zip=" + zipCode;
string strxml;
int shipping = 0;
try
{
using (WebClient client = new WebClient())
{
//Download the xml document as a string
client.Headers.Add(HttpRequestHeader.Accept, "application/xml");
strxml = client.DownloadString(url);
}
XDocument doc = XDocument.Parse(strxml);
XElement generalElement = doc.Element("z2tLookup").Element("errorInfo");
String errorInfo = generalElement.Element("errorMessage").Value;
if(errorInfo.Equals("Success"))
{
XElement generalInnerElement =
doc.Element("z2tLookup").Element("addressInfo").Element("addresses").Element("address");
XElement generalInnerTaxElement =
generalInnerElement.Element("salesTax").Element("rateInfo");
XElement generalInnerNotesElement =
generalInnerElement.Element("notes");
foreach (XElement childElement in generalInnerNotesElement.Descendants("noteDetail").Elements("note"))
{
if (childElement.Value.ToString().Equals("Shipping charges are not taxable"))
{
shipping = 1;
}
}
Response.Write("Zip Code: " + generalInnerElement.Element("zipCode").Value + "
");
Response.Write("Sales Tax Rate: " + generalInnerTaxElement.Element("taxRate").Value + "
");
Response.Write("Post Office City: " + generalInnerElement.Element("place").Value + "
");
Response.Write("County: " + generalInnerElement.Element("county").Value + "
");
Response.Write("State: " + generalInnerElement.Element("state").Value + "
");
Response.Write("Shipping Taxable: " + shipping + "
");
}
}
catch (Exception em)
{
Response.Write(em.Message);
}
}
}
Breakout: sample header for NewC#.NET
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="mssql-breakout.cs" Inherits="CSharpExtended" %>
<!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">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Net;
using System.Xml;
using System.Xml.Linq;
public partial class CSharpExtended : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string zipCode = "90210"; //sample zip code must be between 90001 and 90999
string username = "sample";
string password = "password";
string url = "https://api.zip2tax.com/TaxRate-USA.xml?username=" + username + "&password=" + password + "&zip=" + zipCode;
string strxml;
int shipping = 0;
try
{
using (WebClient client = new WebClient())
{
client.Headers.Add(HttpRequestHeader.Accept, "application/xml");
strxml = client.DownloadString(url);
}
XDocument doc = XDocument.Parse(strxml);
XElement generalElement = doc.Element("z2tLookup").Element("errorInfo");
String errorInfo = generalElement.Element("errorMessage").Value;
if (errorInfo.Equals("Success"))
{
XElement generalInnerElement =
doc.Element("z2tLookup").Element("addressInfo").Element("addresses").Element("address");
XElement generalInnerTaxElement =
generalInnerElement.Element("salesTax").Element("rateInfo");
XElement generalInnerNotesElement =
generalInnerElement.Element("notes");
XElement generalInnerRateElement =
generalInnerTaxElement.Element("rateDetails");
foreach (XElement childElement in generalInnerNotesElement.Descendants("noteDetail").Elements("note"))
{
if (childElement.Value.ToString().Equals("Shipping charges are not taxable"))
{
shipping = 1;
}
}
Response.Write("Zip Code: " + generalInnerElement.Element("zipCode").Value + "
");
Response.Write("Sales Tax Rate: " + generalInnerTaxElement.Element("taxRate").Value + "
");
Response.Write("Post Office City: " + generalInnerElement.Element("place").Value + "
");
Response.Write("County: " + generalInnerElement.Element("county").Value + "
");
Response.Write("State: " + generalInnerElement.Element("state").Value + "
");
Response.Write("Shipping Taxable: " + shipping + "
");
int index = 0;
foreach (XElement childElement in generalInnerRateElement.Descendants("rateDetail").Elements("taxRate"))
{
if (index == 0)
{
Response.Write("Sales Tax Rate State: " + childElement.Value.ToString() + "
");
index++;
}
else if(index == 1)
{
Response.Write("Sales Tax Rate County: " + childElement.Value.ToString() + "
");
index++;
}
else if (index == 1)
{
Response.Write("Sales Tax Rate City: " + childElement.Value.ToString() + "
");
index++;
}
else if (index == 1)
{
Response.Write("Sales Tax Rate Special: " + childElement.Value.ToString() + "
");
index++;
}
}
Response.Write("Sales Tax Reporting_Code Total: " + "(n/a)" + "
");
Response.Write("Sales Tax Reporting_Code State: " + "(n/a)" + "
");
index = 0;
foreach (XElement childElement in generalInnerRateElement.Descendants("rateDetail").Elements("jurisdictionCode"))
{
if (index == 1)
{
Response.Write("Sales Tax Reporting_Code County: " + childElement.Value.ToString() + "
");
}
index++;
}
Response.Write("Sales Tax Reporting_Code City: " + "(n/a)" + "
");
Response.Write("Sales Tax Reporting_Code Special: " + "(n/a)" + "
");
}
}
catch (Exception em)
{
Response.Write(em.Message);
}
}
}
Sample ZIP Code Range
In the sample ZIP code range (90001 - 90999), the following ZIP codes contain multiple tax rates; which can be used to test the return of multiple rows:
- 90304
- 90601
- 90631