title: USPS address validation API : If & or # symbol in address field then API returns error response
body: <p>I have written a C# code to access the USPS Address validation API.
  The API is returning a proper response if there is <strong>no &amp; or # symbol.</strong> But if the request has  # or &amp; symbol then I am getting an error response.</p>
  
  <p>I am using System.Xml.Linq library to build an XML string.</p>
  
  <p><strong>Diff outputs.</strong>
  1)
  <strong>If the address contains #</strong></p>
  
  <pre><code>&lt;Error&gt;
    &lt;Number&gt;80040B19&lt;/Number&gt;
    &lt;Description&gt;XML Syntax Error: Please check the XML request to see if it can be parsed.(B)&lt;/Description&gt;
    &lt;Source&gt;USPSCOM::DoAuth&lt;/Source&gt;
  &lt;/Error&gt;
  </code></pre>
  
  <p>2)  **if the address contains &amp; which is converted to &amp; amp; by XDocument **</p>
  
  <pre><code>&lt;Error&gt;
    &lt;Number&gt;80040B18&lt;/Number&gt;
    &lt;Description&gt;An error has occurred with the service.  Please contact the system administrator, or try again.&lt;/Description&gt;
    &lt;Source&gt;USPSCom::DoAuth&lt;/Source&gt;
  &lt;/Error&gt;
  </code></pre>
  
  <p>Here is the C# code which I have written.</p>
  
  <pre><code>public static XDocument ValidateUSPSAddress(string address1, string address2, string city, string state, string zip)
      {
          XDocument xmlResponse;
          XDocument requestDoc = new XDocument(
              new XElement("AddressValidateRequest",
                      new XAttribute("USERID", UserID),
                  new XElement("Revision", "1"),
                  new XElement("Address",
                          new XAttribute("ID", 0),
                      new XElement("Address1", address1),
                      new XElement("Address2", address2),
                      new XElement("City", city),
                      new XElement("State", state),
                      new XElement("Zip5", zip),
                      new XElement("Zip4", "")
                  )
             )
         );
          try
          {
              var url = Endpoint + VerifyParam + String.Format(XMLParam, requestDoc);
              var clinet = new WebClient();
              var response = clinet.DownloadData(url);
              xmlResponse = XDocument.Parse(System.Text.Encoding.UTF8.GetString(response));
          }
          catch (WebException ex)
          {
              throw ex;
          }
  
          return xmlResponse;
      }
  }
  </code></pre>
  
  <p>and here is a sample request.</p>
  
  <pre><code>http://production.shippingapis.com/ShippingAPI.dll?API=Verify&amp;XML=&lt;AddressValidateRequest USERID="###########"&gt;
    &lt;Revision&gt;1&lt;/Revision&gt;
    &lt;Address ID="0"&gt;
      &lt;Address1&gt;&lt;/Address1&gt;
      &lt;Address2&gt;29851 Aventura &amp;amp; avenue&lt;/Address2&gt;
      &lt;City&gt;&lt;/City&gt;
      &lt;State&gt;CA&lt;/State&gt;
      &lt;Zip5&gt;92688&lt;/Zip5&gt;
      &lt;Zip4&gt;&lt;/Zip4&gt;
    &lt;/Address&gt;
  &lt;/AddressValidateRequest&gt;
  </code></pre>
  
  <p>Any help would be appreciated.</p>