BizTalk Server Tips and Tricks for developers: How to create a .NET class from a schema

  • Sandro Pereira
  • Feb 17, 2021
  • 3 min read

When .NET code makes more sense than adapters

In some scenarios, using BizTalk adapters is not the best option. For example, you may want to handle communication directly through .NET code instead.

Common development scenarios

In other cases, you might need to access multiple or recursive values from a message inside a helper class that supports an orchestration. Likewise, certain situations require complex transformations or message construction that are easier to implement in code than in a BizTalk map.

A simple and reusable approach

Although there are several ways to handle these scenarios, one approach stands out. You can use an XML Schema to generate a .NET class. This method is simple, effective, and works across all these use cases.

By doing so, you gain a quick and straightforward way to represent BizTalk messages as strongly typed .NET classes, making message handling and processing much easier.

📝 One-Minute Brief

Explains how BizTalk developers can generate a .NET class from an XML schema to simplify message handling, enable advanced processing, and perform transformations directly in C# code instead of BizTalk maps.

The easy way to generate classes that conform to a specific schema is to use the need to use the XML Schema Definition tool (Xsd.exe). You can do that by:

  • Open a Developer Command Prompt for VS <edition>
Developer Command Prompt for VS
  • On the command prompt, navigate to the Schema folder and type:
    • xsd /classes /language:CS Schema1.xsd

For complex schemas that import or include other schemas, you need to provide all the dependencies, for example:

  • xsd /classes /language:CS Schema1.xsd Schema2.xsd

Now you will be able to import the C# class generated by the tool to your helper project, and the rest is simple!

On the Orchestration, you can create a variable that represents that class:

  • On the property Type, select the <.NET Class…> and select the class that you created previously:
.NET Class…
  • From the Browse and Select .NET Type to reference, you need to select the proper assembly and the Type.

Then, by using C# code inside Message Assign or Expression Shape, you can serialize or deserialize an XML document into C# and vice versa in a simple and straightforward way:

//CONVERT MESSAGE INTO C# OBJECT
varPersonMsg = msgInput;

//CONVERT C# OBJECT INTO BIZTALK MESSAGE
msgOutput4 = varPersonMsg;

Or if we have a function in a helper class like:

public static XmlDocument MapPersons(Person person)
{
  ...
}

We can directly pass the message to the function without the need to create a variable:

msgOutput4 = Support.Mapping.MapPersons(msgInput);

Hope you find this helpful! If you liked the content or found it useful and would like to support me in writing more, consider buying (or helping to buy) a Star Wars Lego set for my son. 

Thanks for Buying me a coffe
Author: Sandro Pereira

Sandro Pereira lives in Portugal and works as a consultant at DevScope. In the past years, he has been working on implementing Integration scenarios both on-premises and cloud for various clients, each with different scenarios from a technical point of view, size, and criticality, using Microsoft Azure, Microsoft BizTalk Server and different technologies like AS2, EDI, RosettaNet, SAP, TIBCO etc. He is a regular blogger, international speaker, and technical reviewer of several BizTalk books all focused on Integration. He is also the author of the book “BizTalk Mapping Patterns & Best Practices”. He has been awarded MVP since 2011 for his contributions to the integration community.

Leave a Reply

Your email address will not be published. Required fields are marked *

The Ultimate Cloud
Management Platform for Azure

Supercharge your Azure Cost Saving

Learn More
Turbo360 Widget

Back to Top