HowTo: Save and retrieve C# objects in XML

May 4, 2008 by Dave · Leave a Comment
Filed under: Code Snippets 

I’ve spent a bit of time tweaking this XML Serialization class I wrote to make use of the System.Xml.Serialization objects Microsoft provides in .NET 2.0.

XML Serialization is a great way to store complex data types, and also a great alternative to binary serialization. Unfortunately, using these classes is not as straightforward as I would like, there isn’t any simple Save or Load methods, I’ve bridged that gap with a a small class that provides this functionality.

Basic Methods:

These are the two basic methods you would need to save and load an object from a file.

  • public static T Load<T>(string filename)
  • public static void Save<T>(string filename, T cls)

Additional Methods:

These methods are included for those that would like to bypass the files, and send objects directly over a different medium.

  • public static String SerializeObject<T>(T pObject)
  • public static T DeserializeObject<T>(String pXmlizedString)

Important Note:

Remember to wrap all of these methods in try-catch blocks and catch any exceptions thrown. If the file cannot be accessed, or there is an error during serialization, it will be passed on.

Code Example:

Load an object from file:

List MyList = null;
try
{
MyList = XSerial.Load<list>("MyList.xml");
}
catch (Exception e)
{
Console.WriteLine("Error: " + e.Message);
// make sure the object won't cause errors.
MyList = new List();
}
 
Save an object to file:
 
try
{
XSerial.Save</list><list>("MyList.xml", MyList);
}
catch (Exception e)
{
Console.WriteLine("Error Saving: " + e.Message);
// nothing can be done about recovery.
}</list>

Download Demo Project:

XMLSerialTest [ZIP] [Visual Studio 2008]

Tweet from mIRC | mIRC Twitter

May 3, 2008 by Dave · 3 Comments
Filed under: Code Snippets 

Twitter is great, and it’s nice that there is more than one way to send a tweet (message) out, you can use Web, SMS or Google Talk (or even Digsby!). But what if you’re a heavy mIRC user, or if you just like to have more ways to tweet? Clint, on DALnet/#system was working on this, and I’ve added a few things that I think are worth sharing.

How can I use twitter through mIRC?

This would be entered into the Remote script section in mIRC:

alias tweet {
; use /tweet
set %tw.username twitter_username_here
set %tw.password twitter_password_here
; exit if the message is too long for twitter to take.
if ($len($1-) > 140) {
echo -a Sorry, that was $calc($len($1-)-140) characters too long!
halt
}
; connect to twitter on HTTP port 80
set %authentication $encode($+(%tw.username,:,%tw.password),m)
sockclose twitter
sockopen twitter twitter.com 80
; set a 2-second timeout in case there is a problem
; this could be changed to 5 or 10
.timertwitter 1 2 twitter_timeout
set %tweet $$1-
; store the text
}
 
; encoding is important for sending data to a web server
; the decode function is not used, but is here for the sake of
; completeness
alias urlencode return $regsubex($1-,/\G(.)/g,$iif(($prop &amp;&amp; \1 !isalnum) || !$prop,$chr(37) $+ $base($asc(\1),10,16),\1))
alias urldecode return $replace($regsubex($1-,/%(\w\w)/g,$chr($iif($base(\t,16,10) != 32,$v1,1))),$chr(1),$chr(32))
 
; when the socket accepts our connection
on *:sockopen:twitter:{
sockwrite -n twitter POST /statuses/update.json HTTP/1.1
sockwrite -n twitter Host: twitter.com
sockwrite -n twitter User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9b5) Gecko/2008032620 Firefox/3.0b5
sockwrite -n twitter Content-Length: $calc($len($urlencode(%tweet)) + 9)
sockwrite -n twitter Authorization: Basic %authentication
sockwrite -n twitter $crlf
sockwrite twitter status=
sockwrite -n twitter $urlencode(%tweet)
sockwrite twitter $crlf
sockwrite twitter $crlf
; send the postdata
}
 
; the data wasn't returned, kill the socket
; and tell the user.
alias twitter_timeout {
echo -a Message Failed to Send - Socket Timeout
sockclose twitter
}
 
; we have data back
on *:sockread:twitter: {
.timertwitter off
sockread -f %string
echo -a Message Returned ( $+ %string $+ )
sockclose twitter
}

Hopefully this is useful to someone else, I often find myself updating twitter this way, because I always have mIRC open, albeit in the background.

« Previous Page