Thursday, January 10, 2008

Creating an XML Document by Serializing an Object

Let's say we need to create an XML document that looks something like this:

<?xml version="1.0" encoding="utf-8"?>
<
Form>
<
Name>Name</Name>
<
Description>Short Description</Description>
<
Variables>
<
Variable>
<
Name>VarName</Name>
<
Description>Short Description</Description>
<
Type>ListDataSource</Type>
<
Values>
<
Value>
<
ValueMember>1</ValueMember>
<
DisplayMember>New</DisplayMember>
</
Value>
<
Value>
<
ValueMember>2</ValueMember>
<
DisplayMember>Old</DisplayMember>
</
Value>
</
Values>
</
Variable>
</
Variables>
</
Form>



You get the picture, right?



Well, supposing we needed to create these over and over and over and these XML documents are fed into another program for processing.  Perfect time to use serialization, right?  Right.  But that is sometimes easier said that done.  Here's how I go about it.



Step 1: You will need a class the "root level," which in this case would be "FormLayout"  your class declaration would look like this:



[Serializable]
public class FormLayout




Step 2: Create a class for every element that has its own sub-elements.  In our example we would need to create a class for both "Variable" and "Value."  Their class declarations would look like this:



[Serializable]
public class Variable



[Serializable]
public class Value


Step 3: Starting with the highest (most indented?) elements, create a public property for each element under it.  In our case we would start by creating a public property for "ValueMember" and "DisplayMember" (encapsulating private variables of course):



public string ValueMember
{
get { return _valueMember; }
set { _valueMember = value; }
}

public string DisplayMember
{
get { return _displayMember; }
set { _displayMember = value; }
}


Continue with the rest of the classes in the same way...



Step 4: When you arrive at a "Repeating" element, make its declaration in its parent class as an array.  So for instance a "Variable" object would have a repeating list of "Value" elements, so we would create a public property in the "Variable" class that was an array of type "Value".  Like so:



public Value[] Values
{
get { return _values; }
set { _values = value; }
}




NOTE!!!!: It is important at this step to make sure your property names (in this case "Values" is the element that you want surrounding your repeating elements.  We named ours "Values" here because we want our XML Doc to look like:



<Values>




<Value>



...



</Value>



<Value>



...



</Value>




</Values>



Step 5: Mark all of your array properties with the



[XmlArrayItem(ElementName="Foo", Type=typeof(Bar)] attribute.  Where "Foo" is what you want your repeating elements to be, and Bar is the type of array.  In our case we want ElementName="Value" (see XML Snippet above) and Bar = Value.  So our Value[] array in our "Variable" class would look like this:



[XmlArrayItem(ElementName="Value", Type=typeof(Value))] 
public Value[] Values
{
get { return _values; }
set { _values = value; }
}


Serialize Me!



NOTE: You'll need a using System.Xml.Serialization and System.IO for this to work



public void Run()
{
FileStream _fileStream =
new FileStream("c:\file.xml", FileMode.Create);
FormLayout _formLayout = new FormLayout();
// Do something that would fill the FormLayout's properties...
XmlSerializer _xmlSerializer =
new XmlSerializer(typeof(FormLayout));
_xmlSerializer.Serialize(_fileStream, _formLayout);
}



Barring any complications, you should have an XML Document at "C:\file.xml" that follows the structure you need exactly.



Problems Foreseen:




  • Make sure every class is labeled public, otherwise the XmlSerializer can't see it.


  • Make sure every class has an empty constructor, even if you won't be using it.  This is what the XmlSerializer class uses to serialize and deserialize objects.


  • If for some reason you need to have public properties in the classes that you don't want to show up when serialized, mark them with the [XmlIgnore] attribute.

Wednesday, January 2, 2008

F# Programming Language

image Looks like Microsoft has been hard at work on the F# Programming Language, a functional programming language that works on the .NET framework.  Think Python on .NET (yes, I've seen IronPython).  The syntax is definitely not C# or VB like, and at first glance it looks very confusing -- but I think it would be fairly easy to pick up on. 

If I do end up using it, it will probably be for the same reasons I used Python for years; to do one-time tasks programmatically without creating an entire .NET solution.  When I was working help desk I used these "functional programming" tools religiously to read log files, etc.  Automating monotonous tasks is a great way to get started in programming.  Worked for me!

Labels:

Viigo, the best Windows Mobile Application

image Let's face it, browsing the web on a windows mobile device stinks.  Most content isn't made to be viewed on such a small screen and the content that is... well it's usually not very good.

Picsel Browser certainly makes things a lot easier -- which is why it's quickly becoming one of my most used apps -- but it's still a pain to have to zoom in and out, etc.

Enter Viigo, an RSS aggregator for both Windows Mobile and Blackberry devices.  Viigo allows you to create a custom list of RSS Feeds which it updates automatically whenever you open Viigo.  You can choose your feeds from Viigo's built-in list of feeds (nicely categorized for ease of use) or by inputting your own feed URL's.

image

I've had my new Samsung SCH-i760 with Verizon service for a couple of months now and I must say, aside from the same battery woes that plague all smart phones I have been very impressed.  With Picsel Browser, Viigo, and the $45/month unlimited data plan you can accomplish quite a bit without being tethered to that laptop.

Labels:

Friday, October 5, 2007

AJAX Control Toolkit

taskify_your_life Just for kicks I've been writing a new website (the previously mentioned taskify.net) and to a.) Increase the usability of the UI and b.) just learn more about ASP.NET AJAX, I've been implementing a lot of the AJAX controls into the site.  I've got to say, I think Microsoft really hit a home run on this one. 

I'll admit, at first it's very hard to grasp the concepts you must put in place when working with AJAX.NET, but much like .NET as a whole, once you figure out the patterns and the practices used to develop the tools, it's easy to pick up on all of the tools.  You'll never know how satisfying it is to refresh the data in a GridView without a full page refresh until you try it :-)

Head over to http://ajax.asp.net to check out the AJAX Extensions and the Control Toolkit.  You can also click the button below to see a live demo.

image
...but there are problems.  I have had major issues trying to work on an Ajax.net application on my laptop and desktop and syncing my changes.  My DLL references get randomly lost which then throws up an error everywhere that there is an AJAX tag.  A lot of my problems were fixed by changing the TagPrefix property on my ASPX pages to "Ajax" instead of "Asp".  Apparently there is an issue with using Asp as your prefix, but I'm still having issues when copying files over.
 
Usually removing the reference to the DLL and adding it back fixes the problem.  Oh bother.

Labels: , ,

Wednesday, September 19, 2007

Taskify Coming Soon!

taskify_coming_soon

My newest project, Taskify, will hopefully be available pretty soon at http://www.taskify.net (damn those .com squatters)

Taskify is something that I started to write for myself only, a simple, quick, intuitive task list management system.  After thinking about it for a while though, I kept coming up with ideas for some great features, especially along the lines of Text Message Integration, and I'm currently working on putting together a sort of "command line" parser that will really be the the guts of the app.  I think it's going to be pretty sweet, and I hope that it is at least useful to some others.

Monday, September 17, 2007

John Mayer makes Coding Music!

image Let's all face it, John Mayer is probably the greatest musical talent of our generation, and I believe he will go down as one of the all time greats.  He appeals to everyone from 15 year old girls to the hardest of hard-core blues enthusiasts.  You would be hard-pressed to find someone that does a better cover of Jimi Hendrix's "Bold as Love", and his two-part saga in tribute to New York "City Love" and "Covered in Rain" are two of the most expressive, cohesive, and emotional works that I've ever heard.

Most importantly though, John Mayer writes great coding music.  My most productive 10:25 of the day is when "Covered in Rain" randomly shows up on the iPod.  His music has a beat and a rhythm that most music today lacks.  His use of an n'th of a second of silence right before a blaring chord is unparalleled in music today.  If I were going to tell you to to buy 1 CD to try John's music out, I would probably tell you to get Try, a CD he did along with two other musicians as The John Mayer Trio, but his newest work Continuum would also be an excellent choice.  Alternatively, both "City Love" and "Covered in Rain" are featured on his live Any Given Thursday CD.  All of these should be at your local CD shop (if you still have one) or from Amazon.

JM also has quite a sense of humor, which he shows quite often on his blog.

Monday, August 27, 2007

R.I.P. Vista

Well, I've finally done it.  I've gotten so tired of Windows Vista that I've decided to scrap it in favor of XP.  I purchased a copy of XP Pro that should be here in the next week and I can't wait to format Vista!

Problems I had with Vista:

  1. Excruciatingly slow on simple tasks such as moving/deleting files (hours to delete a 100 MB file)
  2. Entire OS as a whole looks good, but the theme makes things hard to read, etc.  There is too much white, everything is white and white. 
  3. Windows classic theme looks like a cartoon.
  4. Several times I shut the computer down only wake up the next morning and find that vista is still "logging off"
  5. Just an overall pause in starting ANYTHING.  It would sometimes take 3-5 minutes to open FireFox.
  6. Don't even get me started on using Visual Studio 2005 on Vista...

Look, I'm a computer programmer, and early adapter, and open minded and smart human being, but there's only so much I can take!!  I am in no way opposed to change, in fact I usually love it (I love Office 2007 for instance) but Vista is bad bits.  Hopefully SP1 will fix most of the issues and if so, maybe I'll give it another try.  For now I'm going to go back to the greatest operating system ever made, Windows XP Professional.

Google Docs -- Web word processing, spreadsheets and presentations. Edit this page (if you have permission) | Report spam