Tuesday, March 18, 2008
New Job, Same Direction
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!
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.
Saturday, October 13, 2007
My GTD Post – Files and Documents Folder Structure
For those of you who don't know, GTD stands for Getting Things Done. I am slowly making improvements in my organization skills in my professional life. Recently I decided to take a closer consideration of my files and documents organization on my Vista laptop. I didn't really have a method to my madness as far as file organization went - some were in folders on my desktop, others were in a folder of the root of C, and yet others were in the profile structure provided by Vista. Two problems with this:
- When I want to backup my important files I have no easy way of doing so.
- The profile file storage provided by Vista gets cluttered with miscellaneous folders such as My Received Files, My Virtual Machines, Visual Studio 2005, etc., etc. It is often the default storage folder for several different programs.
- Contain all folders under one parent folder for easy backup
- A hierarchy that could:
- separate current and past projects
- have a tree for media/docs
So my requirements were as follows:
And this is what I came up with:
I've been using this for about 3 months now and it is working well thus far. For easy access I use shortcuts on my desktop. Additionally these shortcuts allow me to drop files directly into them as if they are folders living on the desktop.
Saturday, May 19, 2007
Script Debugging Alternative in IE
Lately I have been experiencing problems with the Script debugger in Visual Studio 2005. The debugger is started by inserting a debugger statement in your code like so:
function doSomething()
{
for (i = 0; i < 10; i++)
{
debugger;
document.write(i)
}
}
This method of debugging is great because it allows you to debug just as you would with server side code. There are several blogs on the web that go into detail about how to do this. The problem I am having is that the Visual Studio script debugger randomly hangs for no apparent reason.
The other day I was forced to search for a better solution to solve a particular problem I had. One way that I have done this in the past is to use alert() statements, which is just ok at best, so I started asking around at work to see what others have done. One solution that a co-worker of mine (Ryan Garrett) had used was to use document.write()to output information to a new window like so:
var win;
if (!win)
{
win = window.open("","debug","width=400,height=260");
for (i = 0; i < 10; i++)
{
win.document.write(i);
}
}
This is a simple concept, and helped me tremendously in the problem I was solving. The code above instantiates a new window, iterates through a loop 10 times and displays the value of the index on each pass through the loop. The if(!win) check reuses the same window on each pass through the loop. This is important when you have several values you want to output, it prevent a new window from opening up for every value you display. Below is a screenshot of the popup this code creates.
Saturday, May 5, 2007
Gmail and Outlook 2007
I had some trouble setting up Gmail with Outlook 2007. It would receive mail just fine, but sending did not work. Something was up with the SMTP server settings. After enabling POP3 access in Gmail, which you are required to do, I did the following:
- Click Tools à Account Settings to bring up the email accounts dialog window.
- Click New.
- Then it prompts for type of account (Exchange, POP3, etc.). POP3 is default so click Next.
- The next screen prompts for name, email address and password. There is a check box at the bottom to manually configure server settings. By default this is unchecked so that Outlook will run the auto setup. Being a configuration kind of guy I decide to manually set it up to make sure all the settings are correct. So I check the option and click Next. (This grays out name, email address and password).
- I continued with setup per Gmail's configuring your email client: Outlook 2003 (Microsoft had very similar documentation).
So now I'm done, right? Well, sort of. I can receive email, but cannot send. When I click Send/Receive Outlook shows its usual message about connecting to server. And it takes forever. Eventually it times out with a general error about not being able to send mail. In the more settings section in Outlook email account setup, on the advanced tab there is an Outgoing server (SMTP) box where you can enter a port to use. Google and Microsoft both say to use port 465, which did not work. I found a post somewhere on the web to use some other port number (forgot what it was), but that did not work either. I went through and tried every combination of all the other settings that could be causing this to not work correctly but to no avail. I opened new posts on a couple of different high traffic sites to see if anyone could provide some light on the situation. I got several responses with suggestions but nothing worked.
What finally did work for me was to let outlook run through the auto setup – it changed the SMTP port to 587. Now I can send and receive mail! So the lesson here is to try the idiot proof way first, if that doesn't work then manually configure.