HOW TO: Use XmlDocument Elements When Passed to or Returned from WebMethods by Using Visual C# .NET

Article translations Article translations
Article ID: 330600 - View products that this article applies to.
This article was previously published under Q330600
Expand all | Collapse all

On This Page

This article refers to the following Microsoft .NET Framework Class Library namespaces:
  • System.Xml

SUMMARY

This step-by-step article describes how to write code in .NET applications to execute Web Service methods that take an XmlDocument parameter, or that return an XmlDocument object.

Introduction

When XmlDocument objects are passed as parameters to Web Service methods, or are returned from Web Service methods, they are marshaled as XmlNode objects. The XmlDocument and XmlNode types are implemented in the System.Xml namespace (XmlDocument is derived from XmlNode).

If manipulation of the data that is marshaled as an XmlNode (that is passed to a Web Service method, or is returned by a Web Service method) requires that you use the XmlDocument API that is not exposed by the XmlNode type, you must load the data in an XmlDocument object. To do this, use one of the following methods:
  • XmlDocument.LoadXml(XmlNode.OuterXml)
  • XmlDocument.ImportNode(XmlNode, true)
The samples in this article demonstrate how to use these methods

Create a Web Method

  1. In Microsoft Visual Studio .NET, create a new Visual C# .NET ASP.NET Web Service project. To do this, follow these steps:
    1. On the File menu, click New, and then click Project.
    2. In the Project Types list, click Visual C# Projects, and then click ASP.NET Web Service in the Templates pane.
  2. In the Location text box, type XMLDocService to change the default name (the default name is WebService1).
  3. Change the name of the default Web service that is created from Service1.asmx to XMLDocService.asmx.
  4. Click click here to switch to code view in the designer environment to switch to code view.
  5. Define a method that returns an XMLDocument object. Each method that the service will expose must be flagged with a WebMethod attribute. Without this attribute, the service will not expose the method.

    Note Not every method must have the WebMethod attribute. This attribute can be used to hide some implementation details that are called by public Web service methods or if the WebService class is used in local applications. Although a local application can use any public class, only WebMethod methods can be remotely accessible as XML Web services.
  6. Add the following Web Service methods to the XMLDocService class that you just created:
    [WebMethod]
    public XmlDocument GetXmlDocument()
    {
    // Create an XmlDocument object.
    XmlDocument xmlDocumentObject = new XmlDocument();
    XmlDocumentObject.LoadXml("<book genre=\"novel\" publicationdate=\"1997\" " +
    "      ISBN=\"1-861001-57-5\">" +
    "  <title>Pride And Prejudice</title>" +
    "  <author>" +
    "    <first-name>Jane</first-name>" +
    "    <last-name>Austen</last-name>" +
    "  </author>" +
    "  <price>24.95</price>" +
    "</book>"); 
    
    // Return the created XmlDocument object.
    return( XmlDocumentObject );
    }
    [WebMethod]
    public string GetFirstName( XmlNode XmlNodePassed )
    {
    // Create a new XmLDocument object.
    XmlDocument XmlDocumentObject = new XmlDocument();
    
    // Load the XmlNode into the XmlDocument object.
    XmlDocumentObject.LoadXml( XmlNodePassed.OuterXml );
    
    // Find the first name of the author.
    XmlNodeList XmlNodeListObj = XmlDocumentObject.GetElementsByTagName( "first-name" );
    
    // Return the first name.
    return XmlNodeListObj[ 0 ].ChildNodes[ 0 ].Value;
    }
  7. On the Build menu, click Build Solution to build the Web service.
  8. Open the XMLDocService.asmx XML Web service page to test the XML Web service. If you set the local computer to host the page, the URL is http://localhost/XMLDocService/XMLDocService.asmx. The Microsoft ASP.NET runtime returns an XML Web service Help page that describes the XML Web service. You can also use this page to test different XML Web service methods.

Consume the Web Method

  1. In Visual Studio .NET, click New on the File menu, and then click Project.
  2. In the Project Types list, click Visual C# Projects, and then click Console Application in the Templates pane.
  3. In the new application, add a reference to the XMLDocService Web service.

    This step creates a proxy class on the client computer. After the proxy class is created, you can create objects that are based on the class. Each method call that is made with the object is passed to the uniform resource identifier (URI) of the Web service as a SOAP request.
    1. On the Project menu, click Add Web Reference.
    2. In the Add Web Reference dialog box, type the URL of the Web service in the Address text box, and then press ENTER. If you set the local computer to host the Web service, the URL is http://localhost/XMLDocService/XMLDocService.asmx.
    3. Click Add Reference. Alternatively, you can type the URL of the discovery file (XMLDocService.vsdisco) or click Web References on Local Web Server in the left pane to select the XMLDocService service from the list.
    4. Expand the Web References section of Solution Explorer and note the namespace that was used.
  4. Create an instance of the proxy object for the XMLDocService Web service. Paste the following code in the Main function:
    localhost.XMLDocService myXMLDocService = new localhost.XmlDocService();
  5. To invoke the method of the proxy object, use the following code:
    XmlDocument myXmlDocumentObject = myXMLDocService.GetXmlDocument();
  6. On the Build menu, click Build Solution to build the console application.
  7. You receive the following build error message:
    Cannot implicitly convert type 'System.Xml.XmlDocument' to 'System.Xml.XmlNode'
  8. Replace the method invocation with the following:
    XmlNode myXMLNodeObject = myXMLDocService.GetXmlDocument();
  9. On the Build menu, click Build Solution to build the console application. You can successfully build the application.
  10. Load the data in the returned XmlNode into an XmlDocument object using its ImportNode method:
    XmlDocument myXmlDocumentObject = new XmlDocument();
    myXmlDocumentObject.AppendChild(myXmlDocumentObject.ImportNode(myXMLNodeObject,true));
  11. Make a call to the Web Service method that returns the first name of the author:
    string strFirstName = WebServiceObject.GetFirstName( XmlDocumentObject );
  12. Print the first name of the author on the console:
    Console.WriteLine( "The first name of the author is: " +strFirstName );
  13. On the Build menu, click Build Solution to build the console application.
  14. On the Debug menu, click Run to run the console application.

Troubleshoot

If you assign the return value of a Web Service method that returns an XmlDocument object to another XmlDocument object in the client application, you receive the following exception error message:
Cannot implicitly convert type 'System.Xml.XmlNode' to 'System.Xml.XmlDocument'
The error occurs because down-casting is not permitted in Microsoft .NET. The returned XmlDocument is marshaled as an XmlNode. You cannot assign an XmlNode object to an XmlDocument object because XmlDocument is inherited from XmlNode.

Similarly, to expose XmlDocument parameters, you must define the parameters as XmlNode objects. If you declare the parameters as XmlDocument, you receive the following exception error message:
Specified cast is not valid
The exception occurs because the XmlDocument that is passed from the client is marshaled as an XmlNode and the code tries to assign an XmlNode object to an XmlDocument parameter object (down-casting is not permitted in .NET).

REFERENCES

For additional information about how to create Web services, click the following article number to view the article in the Microsoft Knowledge Base:
308359 HOW TO: Write a Simple Web Service by Using Visual C# .NET

Properties

Article ID: 330600 - Last Review: September 4, 2003 - Revision: 6.4
APPLIES TO
  • Microsoft Visual C# .NET 2002 Standard Edition
  • Microsoft Visual C# .NET 2003 Standard Edition
  • Microsoft Web Services (included with the .NET Framework) 1.0
  • Microsoft Web Services Enhancements for Microsoft .NET 1.1
  • Microsoft .NET Framework 1.0
  • Microsoft .NET Framework 1.1
Keywords: 
kbclient kbhowtomaster kbhowto KB330600

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com