XmlDocument class is a very useful class when you want to manipulate XML Document quickly in prototyping mode. But for actually production environment its usage is not recommended. There are lot of reasons for not using it. Memory consumption is one of them because it loads entire DOM in memory so you production memory could fill up fairly quickly if your XML size is relatively large. XmlDocument class has been termed as memory HOG. Here is a very good blog by “Joe Frezner” to give you some idea of comparison of performance of different Xml API’s in .NET.

As per above reference blog it could safely be concluded that XMLWriter and XMLReader are fastest of all XML API’s in .Net not necessarily simplest to implement.  after them we have XDocument(Linq to XML) then we have XmlDocument which 7 times slower than the first one. so if we are looking to use Xml API we should try to find middle ground from DEV, Maintainance and performance perspective and XDocument rightly fits in the middle in most of the cases

So for using XDocument instead of XmlDocument in Biztalk Environment, Following are some points for inter-conversion of types

If you want to manipulate Biztalk Message using XDocument instead of using XmlDocument you could use

following is code to Convert to and from Biztalk message to XDocument Object

XLANGPart to XDocument

public static XDocument GetXDocumentFromXlangPart(XLANGPart messagPart)
return XDocument.Load((Stream)messagPart.RetrieveAs(typeof(Stream)));

XDocument to XLANGMessage

public void SetResponse(XLANGMessage message, XDocument xDocument)
var biztalkMsgStream = new VirtualStream();

Not:- above method takes in XLang message as input parameter we can create our our XLANGMessage object using inherited class. I will give detail some other blog.

  1. I have seen people using XmlDocument class with Biztalk Message in following way just to get XML content from message whereas there is more efficient way of getting XML Content

Instead you could use following code in Utility class to get XML text from XLANGMessage

public static string GetStringFromXlangPart(XLANGPart messagPart)
var reader = new StreamReader((Stream)messagPart.RetrieveAs(typeof(Stream)));
return reader.ReadToEnd();

There are some other items in the list I will update them later

Note:- Above are simple snippet to give idea they don’t contain error checks and exception handling but you should include those as well

Also I have recently implemented impilicit conversion of types by overloading assignment operator in a class I will share that in coming days as well