Thursday, February 14, 2008

How to remove whitespace from xml serialized from a custom object

Recently I came across a problem with whitespace in xml that I was serializing from a custom entity class. The situation is this – I create my custom object, apply the XmlSerializer to it, generate the xml and put it in a memory stream without a hitch. I then convert to a string and save to a database. While verifying the data being saved I find that about %30 of the xml is whitespace, which can become quite considerable when you take into account hundreds of thousands of transactions. My first thought was to use good old regular expressions, but I was concerned about unintentionally removing whitespace that I may want to keep – such as inside an element or attribute. What I finally came up with was to load the xml string into an XmlDocument and set the PreserveWhitespace to false. See below for a simplified example.

Create and populate a custom object:

Car c = new Car();

c.Make = "Jeep";

c.Model = "Wrangler";

c.Year = "1981";


Use the XmlSerializer class to serialize the object as xml to a System.IO.MemoryStream:

System.IO.MemoryStream ms = new System.IO.MemoryStream();

System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(c.GetType());

xs.Serialize(ms, c);


Convert to string for storage in database or other use:


string str = System.Text.ASCIIEncoding.ASCII.GetString(ms.ToArray());


You will now have the following xml in your string variable (it actually has tabs too, but when publishing from Word, blogspot doesn't render the html quite as I expected) So this is nice, right?

<?xml version="1.0"?>

<Car xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">

<Make>Jeep</Make>

<Model>Wrangler</Model>

<Year>1981</Year>

</Car>


Yes and no. The XmlSerializer class is very useful in that it is easy to implement, but it includes whitespace by default. This is good for presentation, but bad for data storage and transmission. The easiest way I have found to strip the white space is to do the following:

Create an XmlDocument and load the string into it:

System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();

xmlDoc.LoadXml(str);


Then set the PreserveWhitespace property to false:

xmlDoc.PreserveWhitespace = false;


Now the .OuterXml property of the XmlDocument will have this:

<?xml version="1.0"?><Car xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><Make>Jeep</Make><Model>Wrangler</Model><Year>1981</Year></Car>

All done! Xml without the whitespace!

Wednesday, February 6, 2008

Linux Finally!

I have tried to install Linux twice in my life before now. The first attempt was with Slackware about 5 years ago - let's just say that didn't go so well. The second was about 4 months ago on a computer I was fixing for family - it was Ubuntu, and although it installed successfully I quickly gave up because I really wasn't looking to spend all the configuration time necessary for a Linux newbie.

A few days ago I decided to install Ubuntu on my current Desktop as a dual boot alongside Vista. My goal is to run everything (outside of my professional computer life as a .NET developer) from Linux. Although it hasn't been without it's bumps, it has gone relatively well. I have it up and running on dual screen monitors, listening to music, browsing the web, watching videos, etc. And it's sooooo much faster than Vista. I still have not committed to going fully to Linux as that will take much more work.

My favorite part of this project is that I am really having fun exploring this technology that is new to me. I have always known Microsoft OS's, from way back in the DOS days, and it's healthy to see other perspectives of how an OS can be run effectively. What has been challenging is the lack of idiot proofing Ubuntu provides. Windows has always excelled at this and tries to prevent you from irreversibly damaging your install (or at least gives you that impression). I reinstalled Ubuntu at least 3 times after hosing the video drivers - but then maybe that is just because I did not know how to unhose them, and it was much quicker to reinstall at that time.

Some Ubuntu tasks I have ahead of me include:
- install WINE so I can run certain windows apps (Quicken comes to mind first)
- setup Picasa for Linux
- research new package that allows read/write to NTFS

Sunday, February 3, 2008

IHE Connectathon

Last week I attended an event called IHE Connectathon. IHE stands for Intergrating Healtcare Enterprise. The Connectathon is an event that is put on by IHE in different countries at various times during the year. I attended the IHE North America Connectathon in Chicago. My purpose for attending was to implement some of the various profiles available in my company's emr application. The goal of our implementation was to be able to exchange xml documents across many different emr systems developed on different platforms with different languages. Anyone in the software development industry can appreciate the enormous complexity of this feat.

Several months leading up to the event my colleague and I prepared vigorously, working long hours. We constructed a database model I was proud of, and the code was not too shabby either. So we packed up and headed off to snowy Chicago ready for the Connectathon. We were quite surprised when Monday morning rolled around and all 300 attendees were there and started buzzing around like insects when the start bell rang. We sat in quiet confusion as we tried to digest the testing process that was happening all around us. After the first day we had figured out how to accomplish what it was we were supposed to accomplish. Mind you, we knew that we had to pass these certain tests, but knowing what our task was and how to accomplish it were two different things that first day.

As the week progressed we fine tuned our system, and dropped a few profiles (which meant less tests to pass), and by the end of the week we had our system functioning properly and passed twenty tests in total. Some companies passed more, some less – all in all it was a successful week and we have had the opportunity to really lay the groundwork for interoperability in my company's application. I discovered that Connectathon really wasn't about passing the tests as much as it was about learning how to become interoperable. In fact, "gold stars" or certification type awards were given out the first few years, but heavy marketing of these types of merits quickly negated the intention of the Connectathon. It created too much competition between the participating vendors, which had a negative effect on the cooperation of those normally competitive companies. You see, at Connectathon, it pays to work with your competitors – if they win then you win and vice versa. While there is a large sense of respect for each company's privacy, there definitely is a focus on working toward win-win situations. I spoke with one infrastructure vendor (one who supports repositories of patient data) who said they do not require anyone working with them to implement and pass the Connectathon tests – they simply have to be able to complete the tasks necessary for the types of transactions they wish to implement.

Where these tests will come into play is when other government-backed health initiatives push certifications forward using the IHE profiles. This is coming – it's a fact! The tests may be in a different form – administered differently, or what have you, but some of the same interoperability functionality we programmed for the Connectathon will be required in the future to have a successful emr application.

Whatever the future holds for me and health care interoperability I am excited to be a part of what it happening. I think it is revolutionary and I think that it will all affect our lives sooner than we realize.