BizTalk – Generating schemas from classes with TargetNamespace

BizTalk provides a us a way of generating schemas from c# classes by using the xsd.exe tool which comes with the BizTalk SDK. The tool also allows us to generate c# classes from a schema and as an option we can specify the runtime namespace for the generated types. What would be nice is if we could specify the target namespace for the schemas when we generate them from our classes. As a BizTalk developer this can cause major headaches as you need to remember to add this manually every time you need to regenerate your schemas. After much investigate it turns out there is a way to get the target namespace into our schemas.

So for example we have our C#’ class which may look something like this:

1using System.Xml.Serialization;   namespace ServiceBusDemo.Model {  
2     public class TodoItem  
3     {  
4         public int ID { get; set; }  
5         public string Title { get; set; }  
6         public string Description { get; set; }  
7         public bool IsDone { get; set; }  
8     } }  
9 

We run it though the xsd.exe tool and it generates us a schemas which looks like this:

- <xs:schema xmlns:b="http://schemas.microsoft.com/BizTalk/2003" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xs:element name="TodoItem" nillable="true" type="TodoItem" />

- <xs:complexType name="TodoItem">

- xs:sequence\

<xs:element minOccurs="1" maxOccurs="1" name="ID" type="xs:int" />

<xs:element minOccurs="0" maxOccurs="1" name="Title" type="xs:string" />

<xs:element minOccurs="0" maxOccurs="1" name="Description" type="xs:string" />

<xs:element minOccurs="1" maxOccurs="1" name="IsDone" type="xs:boolean" />

</xs:sequence>

</xs:complexType>

</xs:schema>

So normally at this point I would go in and manually add the targetnamespace, I probably misspell it, deploy my BizTalk solution and spend half a day wondering why my message are not being routed correctly.

The solution to this is to simply add a reference to the System.Xml.Serialization namespace (Notice how on the msdn help page for the xsd.exe tool it states that the tool uses the System.Xml.Serialization.XmlSerializer) and add the XmlType and XmlRoot (for root elements) Attributes

to your c# classes.

 1using System.Xml.Serialization;   namespace ServiceBusDemo.Model {  
 2     \[XmlType(TypeName = "TodoItem", Namespace = "http://schemas.datacontract.org/2004/07/ServiceBusDemo.Model.TodoItem")\]  
 3     \[XmlRoot(ElementName = "TodoItem", Namespace = "http://schemas.datacontract.org/2004/07/ServiceBusDemo.Model.TodoItem")\]  
 4     public class TodoItem  
 5     {  
 6         public int ID { get; set; }  
 7         public string Title { get; set; }  
 8         public string Description { get; set; }  
 9         public bool IsDone { get; set; }  
10     } } 

Once we have defined the namespace we want to use, rerun the xsd.exe tool and this time all the work is done for us Smile

- <xs:schema xmlns:b="http://schemas.microsoft.com/BizTalk/2003" xmlns:tns="http://schemas.datacontract.org/2004/07/ServiceBusDemo.Model.TodoItem" elementFormDefault="qualified" targetNamespace="http://schemas.datacontract.org/2004/07/ServiceBusDemo.Model.TodoItem" xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xs:element name="TodoItem" nillable="true" type="tns:TodoItem" />

- <xs:complexType name="TodoItem">

- xs:sequence\

<xs:element minOccurs="1" maxOccurs="1" name="ID" type="xs:int" />

<xs:element minOccurs="0" maxOccurs="1" name="Title" type="xs:string" />

<xs:element minOccurs="0" maxOccurs="1" name="Description" type="xs:string" />

<xs:element minOccurs="1" maxOccurs="1" name="IsDone" type="xs:boolean" />

</xs:sequence>

</xs:complexType>

</xs:schema>