How to: Create Web Services That Parse the Contents of a Web Page

This topic is specific to a legacy technology. XML Web services and XML Web service clients should now be created using Windows Communication Foundation.

Web services created using ASP.NET provide an HTML parsing solution that enables developers to parse content from a remote HTML page and programmatically expose the resulting data. For a detailed explanation, see HTML Parsing by ASP.NET XML Web Services.

To specify an operation and input parameters

  1. Create a Web Services Description Language (WSDL) document, which is typically saved with the file name extension .wsdl. The document's content must consist of valid XML according to the WSDL schema. For a prototype, you can use a WSDL document dynamically generated for a Web service running on ASP.NET. Make a request with a ?wsdl argument appended to the Web service URL.

  2. Specify the elements that define the operation each Web service method that parses HTML text. This step and the next one require a knowledge of the WSDL format.

  3. If the parsing method takes input parameters, specify the elements that represent those parameters and associate them with the operation.

To specify the data returned from a parsed HTML page

  1. Add a namespace-qualified <text> XML element within the <output> element that appears via the XPath /definitions/binding/operation/output. The <operation> element represents the Web service method that retrieves parsed HTML.


The operation name inside a binding must be globally unique or Wsdl.exe can be run with the namespace specified to prevent naming collisions caused by other WSDL files imported in the same application.

  1. Add <match> XML elements in the service description within the <text> XML element for each piece of data you want to return from the parsed HTML page.

  2. Apply attributes to the <match> element. The valid attributes are presented in a table under the topic HTML Parsing by ASP.NET XML Web Services.

To generate client proxy code for the Web service

  1. Run the Wsdl.exe tool from the Windows® Software Development Kit (SDK). Pass the WSDL file you created as an input.


The following code example is a simple Web page sample containing <TITLE> and <H1> tags.

  <TITLE>Sample Title</TITLE>
    <H1>Some Heading Text</H1>

The following code example is a service description that parses the contents of the HTML page, extracting the contents of the text within the <TITLE> and <H1> tags. In the code example, a TestHeaders method is defined for the GetTitleHttpGet binding. The TestHeadersmethod defines two pieces of data that can be returned from the parsed HTML page in <match> XML elements: Title and H1, which parse the contents of the <TITLE> and <H1> tags, respectively.

<?xml version="1.0"?>
<definitions xmlns:s=""
    <s:schema targetNamespace=""
      <s:element name="TestHeaders">
        <s:complexType derivedBy="restriction"/>
      <s:element name="TestHeadersResult">
        <s:complexType derivedBy="restriction">
            <s:element name="result" type="s:string" nullable="true"/>
      <s:element name="string" type="s:string" nullable="true"/>
  <message name="TestHeadersHttpGetIn"/>
  <message name="TestHeadersHttpGetOut">
    <part name="Body" element="s0:string"/>
  <portType name="GetTitleHttpGet">
    <operation name="TestHeaders">
      <input message="s0:TestHeadersHttpGetIn"/>
      <output message="s0:TestHeadersHttpGetOut"/>
  <binding name="GetTitleHttpGet" type="s0:GetTitleHttpGet">
    <http:binding verb="GET"/>
    <operation name="TestHeaders">
      <http:operation location="MatchServer.html"/>
         <text xmlns="">
          <match name='Title' pattern='TITLE&gt;(.*?)&lt;'/>
          <match name='H1' pattern='H1&gt;(.*?)&lt;'/>
  <service name="GetTitle">
    <port name="GetTitleHttpGet" binding="s0:GetTitleHttpGet">
      <http:address location="http://localhost" />

The following code example is a portion of the proxy class generated by Wsdl.exe for the previous service description.

' GetTitle is the name of the proxy class.
Public Class GetTitle
  Inherits HttpGetClientProtocol
  Public Function TestHeaders() As TestHeadersMatches
     Return CType(Me.Invoke("TestHeaders", (Me.Url + _
          "/MatchServer.html"), New Object(-1) {}),TestHeadersMatches)
  End Function
End Class
Public Class TestHeadersMatches    Public Title As String    Public H1 As String
End Class
' GetTitle is the name of the proxy class.
public class GetTitle : HttpGetClientProtocol
  public TestHeadersMatches TestHeaders() 
        return ((TestHeadersMatches)(this.Invoke("TestHeaders", 
                 (this.Url + "/MatchServer.html"), new object[0])));
public class TestHeadersMatches 
    public string Title;    public string H1;

See Also


Web Services Description Language Tool (Wsdl.exe)


HTML Parsing by ASP.NET XML Web Services

Other Resources

.NET Framework Regular Expressions
XML Web Services Using ASP.NET