Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / XML

Getting XML Data from Webservice to Infopath

4.00/5 (2 votes)
6 Mar 2011CPOL3 min read 37K   357  
This all about getting XML data through webservices to infopath

Use the Filter Data feature in InfoPath

Create a simple Web service XML data source:

  1. Start Microsoft Visual Studio .NET.
  2. On the File menu, click New, and then click Project.
  3. In the Project Types list, select Visual Basic Projects.
  4. In the Templates list, select ASP.NET Web Service.
  5. In the Location box, type http://<server>/PopulateCities/Service1.asmx?wsdl, and then click Next.
  6. In Solution Explorer, right-click PopulateCities, point to Add, and then click Add New Item. The Add New Item dialog box appears.
  7. In the Templates section, click XML File, type Allcities.xml in the Name box, and then click Open.
  8. Replace everything with the following code, and then click Save.
    XML
    <?xml version="1.0" encoding="utf-8" ?>
    <States>
    	<State value="FL">
    		<ArrayOfString>
    			<string>Coral Gables</string>
    			<string>Miami</string>
    			<string>Orlando</string>
    			<string>Tallahassee</string>
    		</ArrayOfString>
    	</State>
    	<State value="MI">
    		<ArrayOfString>
    			<string>Detroit</string>
    			<string>Flint</string>
    			<string>Lansing</string>
    			<string>Livonia</string>
    		</ArrayOfString>
    	</State>
    	<State value="Unknown">
    		<ArrayOfString>
    			<string>Unknown State</string>
    		</ArrayOfString>
    	</State>
    </States>
  9. Right-click Service1.asmx, and then click View Code.
  10. Add the following code sample to the top of the code page.
    XML
    Imports System.Xml
    Imports System.IO
    Add the following Web service method to the Service1 class.
    // VB.net code
    <WebMethod()> _
    Public Function GetCities2() As System.Xml.XmlDocument
        Dim path As String
        path = Server.MapPath("AllCities.xml")
        Dim doc As New System.Xml.XmlDocument
        doc.Load(path)
        GetCities2 = doc
    End Function

C# Code

C#
[WebMethod()]
public System.Xml.XmlDocument GetCities2()
{
    string path = null;
    path = Server.MapPath("Allcities.xml");

    System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
    doc.Load(path);

    return doc;
}

On the Build menu, click Build Solution.

Create an InfoPath Form

  1. Start InfoPath.
  2. On the File menu in InfoPath 2007, click Design a Form Template. On the File menu in InfoPath 2003, click Design a Form.
  3. Add controls to the new form. To do this, follow these steps:
    1. In the Design a Form Template task pane in InfoPath 2007, click Blank, and then click OK. In the Design a Form task pane in InfoPath 2003, click New Blank Form.
    2. In the Design Tasks task pane, click Controls.
    3. In the Insert Controls list, select Drop-Down List Box. A new drop-down list that is named field1 is added to the form.
    4. In Microsoft InfoPath Designer, right-click field1, and then click Drop-Down List Box Properties.
    5. In the Drop-Down List Properties dialog box, click Add.
    6. In the Value box of the Add Choice dialog box, type FL, and then click OK.
    7. In the Drop-Down List Properties dialog box, click Add.
    8. In the Value box of the Add Choice dialog box, type MI, and then click OK.
    9. In the Drop-Down List Box Properties dialog box, click OK.
    10. In the Insert Controls list, select Drop-Down List Box. A new drop-down list that is named field2 is added to the form.
  4. On the Tools menu, click Data Connections.
  5. In the Data Connections dialog box, click Add.
  6. In the Data Connection Wizard dialog box, click Receive data, and then click Next.
  7. Click Web service, and then click Next.
  8. In the Location box, type http://<server>/PopulateCities/Service1.asmx?wsdl, and then click Next.
  9. In the Select an operation list, click GetCities2, and then click Next.
  10. In the Data Connection Wizard dialog box, click Finish.
  11. In the Data Connections dialog box, click Close.
  12. In InfoPath Designer, right-click field2, and then click Drop-Down List Box Properties.
  13. In the List box entries list in InfoPath 2007, select Look up values in the form's data source. In the List box entries list in InfoPath 2003, select Look up values in a data connection to a database, Web service, file, or SharePoint library or list.
  14. Click Select XPath to the right of the Entries box. The Select a Field or Group dialog box appears.
  15. Add a filter to the return data. To do this, follow these steps:
    1. Expand All Notes, click String, and then click Filter Data. The Filter Data dialog box appears.
    2. Click Add. The Specify Filter Conditions box appears.
    3. In the first drop-down list, click Select a Field or Group.
    4. In the Data Source section, click GetCities (Secondary).
    5. In the dataFields node, Click Value, and then click OK.
    6. Click is equal to for the second drop-down list.
    7. In the third drop-down list, click Select a Field or Group.
    8. In the Data Source section, click Main.
    9. Click field1, and then click OK.
    10. Click OK to close all open dialog boxes.
    11. Click Save to save the form.

Test the Form

  1. On the File menu in InfoPath 2007, point to Preview, and then click Form. On the File menu in InfoPath 2003, point to Preview Form, and then click Default.
  2. Click FL, click MI, and then click Unknown from the first drop-down list. Notice that the second drop-down list updates as expected.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)