C# MSN Chat History Parser
This is a little parser I wrote a while ago with the intention of writing a program to merge my MSN History files together from multiple computers. I haven’t get gotten around to it yet though, so I thought I would share the source. Feel free to use it how you want.
MessageHistory.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
namespace MSNChatParser
{
public class MessageHistory
{
private XmlDocument doc = new XmlDocument();
private string contactsEmailAddress = "";
internal List<Message> messages = new List<Message>();
public List<Message> Messages { get { return messages; } }
public string ContactsEmailAddress { get { return contactsEmailAddress; } }
public MessageHistory()
{}
public MessageHistory(string XMLPath)
{
string fileName = XMLPath.Split('\\').Last();
contactsEmailAddress = fileName.Substring(0, fileName.IndexOf(".xml"));
doc.Load(XMLPath);
Parse();
}
public MessageHistory(XmlDocument Doc)
{
doc = Doc;
Parse();
}
public string ToXML()
{
StringBuilder sb = new StringBuilder();
sb.AppendFormat("<?xml version=\"1.0\"?>");
string Body = "";
int sessionID = 1;
int lastSessionID = 1;
foreach (var item in messages)
{
Body += item.ToXML();
sessionID++;
}
sb.AppendFormat("<Log FirstSessionID=\"{0}\" LastSessionID=\"{1}\">", 1, 1);
sb.Append(Body);
sb.Append("</Log>");
return sb.ToString();
}
public void Parse()
{
XmlNodeList nodes = doc.SelectNodes("//Log/Message");
foreach (XmlNode message in nodes)
{
messages.Add(
Message.FromXML(message)
);
}
}
}
}
Message.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
namespace MSNChatParser
{
public class Message
{
public DateTime DateAndTime { get; set; }
public int SessionID { get; set; }
public string From { get; set; }
public string To { get; set; }
public string Text { get; set; }
public static Message FromXML(XmlNode Node)
{
Message msg = new Message();
msg.DateAndTime = DateTime.Parse(
Node.Attributes["DateTime"].Value
);
msg.From = Node.SelectSingleNode("From/User").Attributes["FriendlyName"].Value;
msg.To = Node.SelectSingleNode("To/User").Attributes["FriendlyName"].Value;
msg.Text = Node.SelectSingleNode("Text").InnerText;
msg.SessionID = int.Parse(Node.Attributes["SessionID"].Value);
return msg;
}
public string ToXML()
{
StringBuilder sb = new StringBuilder();
sb.AppendFormat(
"<Message Date=\"{0}\" Time=\"{1}\" DateTime=\"{2}\" SessionID=\"{3}\">",
this.Date,
this.Time,
this.DateAndTime,
this.SessionID
);
sb.AppendFormat("<From><User FriendlyName=\"{0}\" /></From>", this.From);
sb.AppendFormat("<To><User FriendlyName=\"{0}\" /></To>", this.To);
sb.AppendFormat("<Text>{0}</Text>", this.Text);
sb.AppendFormat("</Message>");
return sb.ToString();
}
public DateTime Date
{
get
{
return new DateTime(DateAndTime.Year, DateAndTime.Month, DateAndTime.Day);
}
}
public DateTime Time
{
get
{
return DateTime.Parse(DateAndTime.ToShortTimeString());
}
}
public override string ToString()
{
return ToXML();
}
}
}
Helpers.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MSNChatParser
{
public class Helpers
{
public static MessageHistory RemoveDoubles(params string[] Files)
{
MessageHistory History = new MessageHistory();
foreach (var File in Files)
{
History.messages.AddRange(
new MessageHistory(File).Messages
);
}
History.messages = (from h in History.Messages select h).Distinct().ToList();
return History;
}
public static MessageHistory RemoveDoubles(params MessageHistory[] History)
{
MessageHistory history = new MessageHistory();
foreach (var hist in History)
{
history.messages.AddRange(hist.Messages);
}
history.messages = (from h in history.Messages select h).Distinct().ToList();
return history;
}
public static MessageHistory LoadFromMultipleFiles(params string[] Files)
{
MessageHistory History = new MessageHistory();
foreach (var File in Files)
{
History.messages.AddRange(
new MessageHistory(File).Messages
);
}
return History;
}
public static MessageHistory MessagesSince(MessageHistory History, DateTime Since)
{
var msgs = (from h in History.Messages where h.Date >= Since select h).ToList();
MessageHistory newhist = new MessageHistory();
newhist.messages = msgs;
return newhist;
}
}
}
How to use
You can either create a new blank MessageHistory object or you can use the Helper to load multiple files into the object
MessageHistory history = Helpers.LoadFromMultipleFiles(“File1.xml”, “File2.xml”)
foreach(var Message in history.Messages){
Console.WriteLine(Message.From);
}
You can also convert everything back to it’s original XML format using the ToXML methods. If you call ToXML from the MessageHistory object it will add all the Messages it contains to the document.
Posted By software development in london at on 12/12/2009
Posted By Fikre at on 30/08/2010