Chapter 8 Message Transformation 207
outputPath += "Patched.xml";
//Set up our parameters for the patch
System.Xml.XmlTextReader sourceReader = new
System.Xml.XmlTextReader changeReader = new
System.IO.StreamWriter outputStream = new
//close the stream that we opened
MessageBox.Show("Your Original Xml has been patched and saved to
'" + outputPath + "'");
All integration solutions can benefit from reducing the size of messages passed
between systems. You have seen that XML Diff and XML Patch are excellent tools
to reduce the size of XML messages in a simple and straightforward manner. With
only a few lines of code, you can gain great efficiencies in your EI solution.
Enhancing and Filtering Message Content
One of the most common reasons for transforming a message is enhancing or
filtering the message content. This simply means adding data to or removing it
from a message. This step usually occurs when the source system or application
does not store the particular piece of data needed to update one of the destination
systems, or when the destination does not accept a piece of data that the message
contains. For example, a bank could have a system that stores loan applications.
When a loan application is retrieved as an XML document, the application might
not contain information about a customer’s credit rating. However, this information
could be retrieved from a credit system and added to the message. The message
can also contain address information, which the target application might not
need. If so, that information could be removed from the message, and this
decrease in size would facilitate faster message delivery.
208 Part III Manipulating Messages
To augment a message effectively, you must know two pieces of informa-
tion. First, you must know where you can obtain the missing data. In other
words, your solution needs to know where certain data is stored or how to find
out that information. Second, you need to know with certainty that the data you
are retrieving from a third-party source is related to the same entity the message
contains. This implies some form of interapplication referential integrity. This
referential integrity is particularly important: if the reference is incorrect, you
will likely add incorrect data to your message and propagate erroneous infor-
mation throughout your enterprise.
If you are working in an industry or company that has some sort of uni-
versal key for the entity across all systems, you are very fortunate. If you do not
have this, you will need to create some sort of identity or cross-reference you
can use for this purpose. There are a few ways to do this. One method is to utilize
a cross-reference table in an external database to hold keys for each entity in
each system on one end of the spectrum to match the values of keys on the
entity at the other end.
Tip You should minimize the need for external keys as much as pos-
sible. If you do not, you could find yourself managing the relational
model of every one of your systems in an external data store. Not a
Once you have the location of the data and a way to identify it, you need
to retrieve it and attach it to the message. You can apply many techniques to do
this. If the message already contains the field for the data but that field is empty,
this process is fairly straightforward. But if the field is not even present in the
message, the process can be more difficult.
If your solution will validate an outbound message (say, against a common
schema), you will need to add the missing part of the message before this valida-
tion occurs. In Microsoft BizTalk Server 2004, this might happen in the receive
pipeline. If you are using an XML schema, you will need to have a before
schema and an after schema, or you will need to add a namespace for each sec-
tion you want to add if such a namespace is not already present. Ensure that all
the elements you require are included in the schema up front to minimize the
different permutations of a message schema that can occur.
For example, assume you will start with an XML message that resembles
the following listing. We will demonstrate how to read this XML message into a
DataSet to easily manipulate it in the code that follows.