Code Sample – Retrieve Domain Values from WQX

Introduction

EPA provides a web service that allows systems to retrieve the most up-to-date listing of domain values used by the WQX data flow. Domain values for a variety of data elements (e.g Characteristic Names, Unit Codes, Analytical Method Codes, etc) are made accessible by EPA to assist partners in conforming to a consistent nomenclature. Click here for more information on this service.

Note: this service does not comply with the Exchange Network 1.1 or 2.1 specifications, but is widely used in conjunction with an Exchange Network service (WQX).

Service Description

One service (called GetDomainValues) is provided as descibed in the WSDL here. The GetDomainValues service has one optional parameter called DomainName. If DomainName is left blank, domain values for all fields are returned. If DomainName is supplied, only the values for the specified domain are returned. Refer to the bottom of this page for the values to supply for the DomainName parameter. 

Information is returned in a zip file containing one XML file called “Results.xml”.

Step-by-Step Code Sample 

This is a sample of calling this service using .NET C# and Visual Studio 2010.

1. Using Visual Studio, start a new C# project

2. Add a Web Reference to the remote service:

In Visual Studio, select the menu Project –> Add Service Reference

Click the Advanced button. Then click the Add Web Reference button. In the URL box, enter in the URL to EPA’s WSDL for this service (http://cdx.epa.gov/WQXWeb/services.asmx?WSDL), click the green arrow button. Visual Studio will then generate the necessary classes to interact with the remote service. Finally, click the Add Reference button.

3. Call the service to retrieve domain values:

Add the following using directive:

using WQXGetDomainValues_Sample.gov.epa.cdx;

Then add the following 2 lines to retrieve data as a byte array:  

DomainValuesService d = newDomainValuesService();
byte[] b = d.GetDomainValues(“MeasureUnit”);

 4. Extract the XML from the returned ZIP file. (Until .NET framework 4.5 is released, you’ll need to reference a 3rd party utilty to manage zip files. There are different utilities out there. This example uses DotNetZip.)

Download the DotNetZip package.

Reference the Ionic.zip.reduced.dll in your solution.

Add the following using directive:

using Ionic.Zip;

Add the following code to unzip

string dir = @”C:\Temp”;
using (System.IO.Stream stream = new System.IO.MemoryStream(b))
{
using (var zip = ZipFile.Read(stream))
{
foreach (var entry in zip)
entry.Extract(dir);
}
}

5. Parse the response XML. There are many ways you can manage the data that is returned. The following code provides just one example:

Add the following using directive since this uses Linq to XML to parse the XML file:

using System.Xml.Linq;

Then add the following code. Notice that it references the WQX XML namespace to properly retrieve data:

XDocument xdoc = XDocument.Load(System.IO.Path.Combine(dir, “/Results.xml”));
var lv1s = from lv1 in xdoc.Descendants(“{http://www.exchangenetwork.net/schema/wqx/2}WQXElementRow“)
select new
{
ID = lv1.Descendants(“{http://www.exchangenetwork.net/schema/wqx/2}WQXElementRowColumn“).First(ID2 => ID2.Attribute(“colname”).Value == “Code”).Attribute(“value”),
Text = lv1.Descendants(“{http://www.exchangenetwork.net/schema/wqx/2}WQXElementRowColumn“).First(Text2 => Text2.Attribute(“colname”).Value == “Description”).Attribute(“value”),
};

foreach (var lv1 in lv1s)
{

//store to database here
string value = lv1.ID.Value;
string text = lv1.Text.Value;

}

The foreach loop above can then be used to read into your datasource or handled in any other way you wish.