Connecting to API Using C#.NET

C#-Logo 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.

Basic: sample header for NewC#.NET

		<%@ 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>

		
Basic: mssql-basic.cs

		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>

		
Breakout: mssql-breakout.cs

		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
Terms of service: At Zip2Tax, your security and confidentiality are our highest priority. We will never share your e-mail address or any other personally identifiable information (PII) with any other person or company. Please review our full privacy policy. License: Please refer to our end user license agreement. Cancellation and Refund Policy: Please refer to our refund policy prior to purchase.
Copyright © 2006-2018 Zip2Tax, LLC. All rights reserved. Zip2Tax and Spot-On™ are trademarks of Zip2Tax, LLC, reg. pending U.S. Pat. & Tm. Off., S/N 86/613550 and 86/613547.

Quantcast
Quantcast
IP Address: 34.204.52.4
JavaScript: Fail
loggedd: none
WSPhilly03/DBPhilly03/CCProd

Don't have an account?

Sign up for a free trial here
If you do not have an account, please provide the information below.


Already have an account?
Free trial close Thank You!

Please check your email for your Username and Password.

If you do not receive your email shortly, check your SPAM folder for mail from "info@Zip2Tax.com" or call us at 1-866-492-8494, Monday - Friday 8am - 5pm EST.
Zip2Tax Password recovery close Password recovery

Please provide your registered email to verify and send password.



submit