ObjectSharp Blogs

You are currently viewing

Matt Meleski's .Net Blog - The ABC's of .NET

ASP.NET/ADO.NET/Biztalk 2004/C#


Passing an XML instance to a Web Service Method via a BizTalk 2004 Orchestration Web Port -> Choices.

Need to call a web method from a BizTalk 2004 Orchestration Web Port and pass an instance of XML to the web method.
Below are some options:


1) Pass the XML as a string.

For example your Web Method that you need to call from your orchestration might look something like this:

[WebMethod]
ProcessOrder (string myOrder)
{

 System.XML.XMLDocument myOrderDom = new System.XML.XMLDocument()
 myOrderDom.load(myOrder)
   // Now have code to process the order
 System.Xml.XmlNodeList nodeList = myOrderDom.GetElementsByTagName("OrderItems");
 System.Diagnostics.Debug.WriteLine("The number of Order Items passed in with the XMLDocument is: " + nodeList.Count.ToString());
 foreach(System.Xml.XmlNode orderItemNode in nodeList)
 {
  System.Diagnostics.Debug.WriteLine("The order item node is : " + orderItemNode.OuterXml);

 } 
 

}

In the Orchestration :
To create the XML string representation of the message in your orchestration:
a) In your orchestration create a variable of type System.XML.XMLDocument

System.XML.XMLDocument varXMLDOM

b) In an expression shape assign the XMLDocument type variable to the message that
you want to pass to the XMLDocument.

varXMLDOM = msgIncomingOrder;

c) Finally set the message that contains the string parameter that will be passed to the Web Method:

msgXMLStringRequest.XMLAsString = varXMLDOM.OuterXml;


2) Pass the XML as a System.XML.XMLDocument
For example your Web Method that you need to call from your Orchestration might look something like this:


[WebMethod]
ProcessOrder (system.XMl.XMLDocument myOrder)
{

 System.Xml.XmlNodeList nodeList = myOrder.GetElementsByTagName("OrderItems");
 System.Diagnostics.Debug.WriteLine("The number of Order Items passed in with the XMLDocument is: " + nodeList.Count.ToString());
 foreach(System.Xml.XmlNode orderItemNode in nodeList)
 {
  System.Diagnostics.Debug.WriteLine("The order item node is : " + orderItemNode.OuterXml);

 }

}


In the Orchestration :
To set the message that contains the XMLDocument parameter that will be passed to the Web Method , simply set the web message to the orchestration message :
msgXMLDomRequest.XMLAsXMLDocument = msgIncomingOrder;


3) Pass the XML as a Strongly Typed Class

Your order class (defined on the Web Service side) might look something like this :

[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://PassingXMLToWS.Orders")]
[System.Xml.Serialization.XmlRootAttribute(Namespace="http://PassingXMLToWS.Orders", IsNullable=false)]
public class Orders
{   
 [System.Xml.Serialization.XmlElementAttribute("OrderItems", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
 public OrdersOrderItems[] OrderItems;

 [System.Xml.Serialization.XmlAttributeAttribute()]
 public string OrderId;
   
 [System.Xml.Serialization.XmlAttributeAttribute()]
 public string TotalAmount;
}


[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://PassingXMLToWS.Orders")]
public class OrdersOrderItems
{
   
 [System.Xml.Serialization.XmlAttributeAttribute()]
 public string OrderItemId;
   
 [System.Xml.Serialization.XmlAttributeAttribute()]
 public string ItemAmount;
}

Note: I created the above class by using the xsd utility that is installed with VS.Net.
a) I created an xsd BizTalk Schema, and called it Orders.xsd
b) I then generated the above class, by opening up the Visual Studio Command Prompt and then
typing in the following :
c:\passxmltowebservice> xsd Orders.xsd /c /l:cs
c) The xsd utility then created a class for me called, Orders.cs, which I then copied over to
my web service project.


Your Web method that accepts an Order class might look like the below:

[WebMethod]
public void AcceptOrderClass(Orders myOrders)
{
 System.Diagnostics.Debug.WriteLine("In AcceptOrderClass");
 System.Diagnostics.Debug.WriteLine("The Id of the Order passed in is: " + myOrders.OrderId);
 foreach (OrdersOrderItems orderItem in myOrders.OrderItems)
 {
  System.Diagnostics.Debug.WriteLine("The Id of the Order Item is " + orderItem.OrderItemId); 
 }
}


In your BizTalk project, when the Web Reference is added,
an XSD schema will be created that represents the Orders class (Called Reference.xsd).
Inside your Orchestration you create a message of type Reference.xsd and then construct this message, just like any other message.
When your message reaches the web service side it will automatically be converted to an instance of -> public class Orders  


Here is a sample of each.

 

 


 

Comments

  • matt November 30, 2004 3:00 AM

    Thanks for your sample.
    but, how can i know the orchestration was done successly?

  • matt October 20, 2005 12:35 PM

    Thanks Matt! I cannot tell you how helpful I find your blog! Keep up the good work!

  • matt March 19, 2006 11:51 PM

    Without Using XML DOM how to a XML as a String

    - Bala

  • matt March 20, 2006 4:36 PM

    Please start by reading this blog entry:

    http://blogs.msdn.com/darrenj/archive/2004/09/29/235719.aspx

    You can call a .Net component and then get at the contents of the BizTalk message. Or do the same in a custom pipeline component

Leave a Comment

(required) 
(optional)
(required) 

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS