Dev

Tutorial: C# XML Serialization

App, Dev , , , , , , , ,
0

Almost every decent sized project in any programming language winds up having at least one configuration file. XML is a standard we often rely on because it’s generally perfect for the job. However, most people end up simply working with the XML file like they would any other file or simply scanning along each part of the “tree”. XML Serialization can take care of a lot of these instances with much better results, by providing an actual class interpretation of the configuration file. Let me show you.

Source Code

Reviewing Code Line by Line

I know that on this web page, the code looks like quite a lot for some of you out there, but it’s actually not. It’s honestly quite small. We’ll start with the “imports”. These 5 imports are the only imports you’ll actually need. You won’t need them on every page, but if you download the source, you’ll see where each is needed/used. You needs the Collections series for the Dictionary we are going to make and the IO so we can read/write our configuration file.

See? It’s already starting off fairly simple. Next, we create a class that is going to be our config. This means we have to design how each component is going to match up to it’s counterpart in the XML config. To be honest, this class is going to also generate the config, so that we’ll never even need to manually edit the config file, but we could if we wanted to.

The first thing we have to do is define our root. All we are simply doing is saying that the root of our configuration file will be “ConfigRoot”.

Next, we’ll define an ArrayList we are going to make as well as a Dictionary. I could explain how this works, but I think the code example shows it pretty clear. Kids will be the name of the “ArrayList” in the config and will have a series of kid‘s within it.

The one thing I would like to note however, is that the reason I am making both an ArrayList and a Dictionary for something that should seemingly just need a Dictionary is because currently you can’t Serialize a Dictionary, but you can Serialize a List. There are Dictionary classes that have been created to be Serialized or ways to get around this issue, but this tutorial is more about showing you the basics of XML Serialization, not a tutorial showing you how to most efficiently get a Dictionary through Serialization and into an XML Config, etc.

We’re going to make some generic methods to add a kid, etc but the main two I want you to take a look at now are the two methods we use to create and work with our Kid Dictionary.

We’re going to create a method called GetKid() to retrieve a Kid by name. Then we’re going to create a CreateKidDict() method. We’ll simply loop through the Kid‘s in our Kid ArrayList and then insert into the Kid Dictionary with their Name as the key.

The last two methods within this class are pretty self explanatory as well. We are just creating a method to Serialize our Config class to XML. Then, we’ll call the method to De-serialize the XML back into an instance of the Config class. The only note I’d like to make is the fact that we call the CreateKidDict() method on our new Config before returning it. The reason to that is because we want the Dictionary of our Kid‘s to already be pre-populated.

Okay, we’re onto the next and final class before the Implementation stage. There really only needs to be two things pointed out in this class, so I’m just going to show them and not the whole class.

Take a look and you’ll notice that we’re creating XmlElement‘s.

This way both Name and Age are stored for each Kid in our XML config.

Implementation

I’m going to briefly go over this class as anyone who has been programming for any significant amount of time should know how to make method calls on classes, etc.

  1. We create three new Kid()‘s.
  2. We create our Config() instance to be used for output.
  3. We add all three Kid()‘s to our Config().
  4. Finally, we output our Config() using our SerializeToXML() method.

Now, in a real circumstance(real life situation), we probably wouldn’t simply write out a config and then decide to immediately read it back in on the next line, but that’s what we’re going to do here since I’m simply trying to show each feature off that we have just created.

So, what we’re going to do is:

  1. De-serialize the Config() we just saved by calling our method DeserializeFromXML().
  2. Loop through all the Kid()‘s and print out their Name‘s to make sure we got them all.
  3. As a final test, get our GetKid() method to pull each Kid()‘s Age from our previously created Kid Dictionary.

Well, that’s all there is to it! Hope this all makes sense. If you have any trouble, simply download the source code provided at the top of this article and mess around with it. See what works and doesn’t work when you change things. That is honestly how I learn most tips and tricks, by tedious trial and error.

One last thing I’d like to mention is that I know there is probably an easier way(and more optimized) to do the whole Config() class structure, but this demonstration is for the purpose of simply learning the basics of XML Serialization. It can be taken much further than this, but that would be out of the scope I’m aiming for.

LangFreQ Beta v1.1 – New Features

Dev, News, Web , , , ,
0

I haven’t been posting much at all lately and mostly it’s because I’ve been so busy with the holidays, but also because I wanted to crank out the new LangFreQ Beta v1.1 with new features. I’ve been working hard to get these out and while I think these features are a vast improvement on the site, or an addition if you will, I think it will be a long time before I say LangFreQ is out of “beta”.
(more…)

LangFreQ – Beta

Dev, News, Web , , , , ,
0

Lately, I haven’t been as active as I would like. Part of that is because I’ve been so busy on a new little project of mine, the other part is that I’m studying Japanese so much. 😉 I’d like to share with you the project I’ve been tinkering with, it’s called LangFreQ!

Overview

LangFreQ is a website that allows you to find the popularity of words in a given language, using Twitter data. Currently LangFreQ is beta so it only supports three languages: English, Spanish, and Japanese(of course it does Japanese.. I’m studying it..). The results of a word not only show the ranking but they also show the word translated into several other languages. If LangFreQ seems to be relatively useful for a decent amount of people, I’ll definitely add more languages.

Top Words & Search

most-frequent-wordsTop Words is the easiest way to browse the current top words of a language, so that you can simply click page by page and view the results. There exist a top bar above the results so that you can change what language you wish to view the top words for. You can also set the Per Page, so that you only see so many words per page, as well as set the amount of top words you’d like to view. Maybe you only want to see the top 100 words? Or maybe you want to see the top 1000? Set it to what you like.

Currently, there aren’t even 1000 words in the English section. Why is this? Well, it’s beta, and because it’s beta I am testing word lists out. I’ll later “upgrade” to greater word lists and therefore there will be many more results.

The search is pretty obvious. You simply type in a word you’d like to see the ranking for. However, you can also type multiple words with a space in between to view them all at the same time.

Compare

compare-phrase-popularityCompare is an interesting utility. Basically, you type in a word or phrase and it uses Google Search to determine how many results there are for each word. Which ever has more results is the more popular phrase. One great feature about this tool is that you may want to know what is the more natural way something is said in a given language, or more common way. Often there are more than one way to express something, but generally there is always one common way that is used more than the others.

Twitter Bot

Last but not least, there is the LangFreQ twitter bot. This tool was generated because Brett over at Rainbowhill gave me this great idea. Definitely go check his blog out, this guy is full of ideas, especially in the Japanese language department! Anyways, the twitter bot is basically just as it sounds.. it’s a bot on twitter that will allow you to ask it to give you the rank of a word, translate a word or phrase from one language to another, or even identify the language of a given word or phrase! You don’t have to follow LangFreQ twitter bot(@langfreq) to use it, but if you’d like to support this project, please do! If you need help with knowing which commands the bot has available, head on over to the @langfreq twitter bot manual. There are more features to be had in this area as well. So, if you think of any feature you’d like for the twitter bot to have, then please mention them to me!

Conclusion

That’s all I wanted to cover with you. Just wanted to basically mention my new beta project, so we’ll see how it works out and if people like it or not. Please, let me know if you find this project horrible, useful, interesting, etc. ALL comments and suggestions will be appreciated.