Click here to Skip to main content
15,891,253 members
Articles / Programming Languages / C#
Article

Combine multiply MSN chat histories into whole one

Rate me:
Please Sign up or sign in to vote.
3.48/5 (8 votes)
4 Feb 20064 min read 56.8K   561   12   12
It can combine multply MSN chat histories into whole one and guide you how to use XML.
Sample Image - CombineMSNChats.jpg

Introduction

Generally, many people maybe use MSN Messenger in different places, such as home, office, and other places. it will result in a problem that multiply MSN chat histories are in difference places. It is good if these copies of MSN chats can be combined into one. But MSN Messenger doesn't support this function, So I program it myself. This tool is programming in C# using .NET framework 1.1.

The Method To Implement It

We must get know the MSN chat history before we do it. As we know, the chat is saved in XML file. Due to this, we should know the format of the XML file. The XML file can be opened in Editor tools, such as UltraEdit, XMLSpy. OK.I'll show a picture here to get know the formats for MSN chat file. I use XMLSpy to operate on it. because XMLSpy is a very excellent tool on XML.

First, see the picture below:

  Sample screenshot

In this picture, we can find that MSN chat file has a root element <Log>, and many child nodes—one MSN message item --are attached to the root element.<Log> element has 2 attributes, FirstSeesionID and LastSessionID, which indicate the session.

 Obviously, there are 3 types of MSN message. Actually, there are 5 types:

  • Common Message
  • Invitation Message
  • InvitationResponse Message
  • Leave Message
  • Join Message

Different type of MSN messages has different formats. I’ll demonstrate it as following.

  • Common Message

Let’s take a look at a Common Message example.

<Message Date="2005-11-6" Time="15:16:17" 
DateTime="2005-11-06T07:16:17.339Z" SessionID="1">
<From> 
<User FriendlyName="From User"/> </From>

<To><User FriendlyName="To User "/> </To>

<Text Style="font-family:MS Shell Dlg; color:#000000; ">Text </Text>

</Message>

 You can get the schema of this easily. Refine it:

<Message Date="" Time="" DateTime="" SessionID="">

<From><User/></From>
<To><User/></To>
<Text/>
</Message>

A common message includes 3 parts: From User, To user, and Message text.

  • Invitation Message

We can use the same way to fetch the format.

<Invitation Date="" Time="" DateTime="" SessionID="">

<From> <User FriendlyName=""/>
<User FriendlyName=" "/>
</From>
<Application> 
</Application>
<Text 
Style=""> 
</Text>
</Invitation>

So it also includes 3 parts: From User, Application (Or File), Text.

  • InvitationResponse Message
  
<InvitationResponse Date="" Time="" 
DateTime="" SessionID<From>
<User FriendlyName=" "/>From><Application> 
</Application><Text 
Style="<SPAN lang=EN-US style="FONT-SIZE: 12pt">"> 
</Text></InvitationResponse>

Same Invitation Message

  • Leave Message
<Leave Date="" Time="" DateTimne="" SessionID="">
<User FriendlyName="">
<Text Style=""></Text>
<?Leave>

It includes 2 parts: User and Text.

  • Join Message

Same as Leave Message.

After we know the format of MSN chat file, we can get each message from multiply copies of MSN chat. Since each message has unique date time and session, we can sort the MSN messages by them.

Then save these sorted MSN message into a container, such as ArrayList, hashtable and SortedList in .NET framework.

 Finally, save these sorted MSN messages in a file in XML Format.

 In conclusion, there are 4 steps to it

  •   Step 1: Read the copies of MSN chat file and parse the message.
  •   Step 2: Sort MSN messages
  •   Step 3: Combine these copies of MSN message.
  •   Step 4: Save the sorted MSN message to a XML file.

 

Generate Session ID

  Because the MSN message come from different files, so the Session ID maybe are same. How to generate the session ID? As we know, the date time of each message is different. We can use it to do this.

   In the original MSN chat file, previous and current MSN message maybe have same session ID, if so, the session of them should be same,. If not so, the current message should be previous message’s session ID +1.

 

How to implement it.

 I use C# to do this. In .NET framework, it is easy to operate on XML file.

 I define 5 classes to represent the types MSN messages. They are:

  • MSNInvitationMessageInfo

To represent MSN invitation message.

  • MSNInvitationResponseMesssageInfo

To represent MSN invitation response message.

  • MSNJoinMessageInfo

To represent MSN join messages.

  • MSNLeaveMessageInfo

To represent leave message.

  • MSNMessageInfo

To represent common message.

These 5 classes all inherit from a base class: MSNBaseMessage.

Meanwhile, I also declare other 3 class:

  • MSNChatDocument

Represent a MSN chat history file.

It includes 3 important properties:

             FirstSessionID—First Session ID

LastSessionID—Last Session Id

MSNMessages—All MSN messages from a MSN chat history file

  • MSNDocumentCombine

Combine multiply copies of MSN histories.

It has 4 important properties:

                   FirstDocumentPath—The first MSN history file path.

                  SecondDocumentPath—The second MSN history file path.

                  SavedDocumentPath—The path to save the combined MSN history

                 XSLFilePathSrc—XSL file path.

And a method—Combine()—to combine MSN files.

  • MSNSession

Generate Session ID.

To get the overview, see the below picture

Image 3

Source Code

Let’s have a glance at the UI entry

MSNDocumentCombine chatDoc=new MSNDocumentCombine();
chatDoc.FirstDocumentPath=listFiles.Items[0].SubItems[1].Text;
chatDoc.SecondDocumentPath=listFiles.Items[1].SubItems[1].Text;
chatDoc.SavedDocumentPath=this.txtSavePath.Text;
chatDoc.XSLFilePathSrc=this.txtXSLPath.Text;
chatDoc.Combine();
for(int index=2;index&alt;listFiles.Items.Count;index++)
   {  
   chatDoc.FirstDocumentPath=this.txtSavePath.Text; 
   chatDoc.SecondDocumentPath=listFiles.Items[index].SubItems[1].Text;
   chatDoc.SavedDocumentPath=this.txtSavePath.Text;
   chatDoc.Combine(); 
}   

Create a MSNDocumentCombine object, then set value to its properties, finally call Combine() methods to implement it.

For the details to combine. See the below code:

//Load the first document
firstDoc=new MSNChatDocument(this.FirstDocumentPath);
firstDoc.Load();
//Load the second document
secondDoc=new MSNChatDocument(this.SecondDocumentPath);
secondDoc.Load();

//Combine the 2 files records
SortedList sl1=firstDoc.MSNMessages;
SortedList sl2=secondDoc.MSNMessages;
if(sl1.Count<=sl2.Count)
{
 for(int index=0;index&alt;sl1.Count;index++)
  {
   if(!sl2.ContainsKey(sl1.GetKey(index)))
   {
   sl2.Add(sl1.GetKey(index),sl1.GetByIndex(index));
   }
 }
//save to new XML file
Save(sl2);
}
	.......
     }

First, load the files, and parse the MSN messages.then combine 2 MSN messages source. Finally save these message.

How to save the message? I have mentioned that there are 5 types MSN messages and 5 MSN message format. so we can save it according to the format.

Take Commmon MSN Message as an example:

/// <summary>
// Build common message.
/// </summary>
/// <param name="sw">Stream witter to XML file</param>
/// <param name="message">The common message.</param>
private void BuildGeneralMessage(StreamWriter sw,MSNMessageInfo message)
{			
//Message
sw.WriteLine("<Message Date=\""+message.DateTimeOn.ToShortDateString() 
+"\" Time=\""+ message.DateTimeOn.ToShortTimeString()+"\" DateTime=\""

+message.DateTimeOn.ToString("s")+"."+message.DateTimeOn.Millisecond
+"Z\" SessionID=\""+message.SessionID+"\">");
			
//From User
sw.WriteLine("<From>");
for(int index=0;index<message.FromUsers.Count;index++)
{
 sw.WriteLine("<User FriendlyName=\""+

ToXmlString(((MSNUserInfo)message.FromUsers[index]).FriendlyName)+"\"/>");
}
sw.WriteLine("</From>");
				
//To Users
sw.WriteLine("<To>");
for(int index=0;indexlt;message.ToUsers.Count;index++)
{
sw.WriteLine("<User FriendlyName=\""+ToXmlString((((MSNUserInfo)
message.ToUsers[index]).FriendlyName)+"\"/>"));
}
sw.WriteLine("</To>");
//Text
sw.Write("<Text ");
if(message.Text.Style!=null)
{
sw.Write("Style=\""+ToXmlString(message.Text.Style)+"\">"+
ToXmlString(message.Text.Text)+"</Text>");
}
else
{
sw.Write(">"+ToXmlString(message.Text.Text)+"</Text>");
}
sw.WriteLine();

//Bottom
sw.WriteLine("</Message>");
}

First, build Message entity, then create From user, To user and Message text. So other types are .

How to generate Session ID? We can get the code from class MSNSession.

int nSessionID=1;
int nOldSessionID=0;

//There are more than 1 MSN messages
for(int index=0;index<m_slSrc.Count;index++)
 {
  me=new MSNBaseMessage();
  me=m_slSrc.GetByIndex(index) as MSNBaseMessage;

  if(index==0) 
  {	
   nOldSessionID=me.SessionID;
   me.SessionID=1;
   nSessionID=1;					
  }
  else
  {
   pre=new MSNBaseMessage();         
   pre=( MSNBaseMessage)(m_slSrc.GetByIndex(index-1));			

 if(me.SessionID==nOldSessionID&&me.FilePath.Equals(pre.FilePath))
   {
    me.SessionID=nSessionID;
  }
 else
 {
  nOldSessionID=me.SessionID;
  me.SessionID=nSessionID+1;
  nSessionID++;
  }
 }
}

Conclusion

We must know the Format of MSN chat history. Once get it, it will become easy to do it. And during it, It is important to read, parse, and save the MSN messages. In additional, How to generate the session ID also is important. XSL file is the style of MSN message. Please DO copy it to the MSN history file.

History

  •  2006-01-30 The initial version.
  •  2006-02-05  Updated. Update the generation of session ID

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
China China
I'm from Shanghai, China, and working on a company Tangerine Technology. Like Softwate development,open source, enterprise development. In addition, I spend much time translating Enlish book , technical articles.
I like reading book, sports and tour, and listening music. the feeling is so good!

Comments and Discussions

 
GeneralAbout "InvatationResponse", Serious ERROR to make me lose or damage my messages [modified] Pin
Harly14-Feb-10 13:56
Harly14-Feb-10 13:56 
Generalbug with quotes in nicknames Pin
852y3agnna14-Jun-06 13:24
852y3agnna14-Jun-06 13:24 
QuestionTime Stamp Not Working? Pin
Coke355mL8-Feb-06 3:23
Coke355mL8-Feb-06 3:23 
AnswerRe: Time Stamp Not Working? Pin
Taiguo8-Feb-06 14:07
Taiguo8-Feb-06 14:07 
GeneralRe: Time Stamp Not Working? Pin
Coke355mL20-Feb-06 13:59
Coke355mL20-Feb-06 13:59 
GeneralRe: Time Stamp Not Working? Pin
Taiguo20-Feb-06 14:24
Taiguo20-Feb-06 14:24 
GeneralUpdate this article Pin
Taiguo5-Feb-06 3:14
Taiguo5-Feb-06 3:14 
QuestionGood job! Pin
flair5-Feb-06 2:38
flair5-Feb-06 2:38 
AnswerRe: Good job! Pin
Taiguo5-Feb-06 2:51
Taiguo5-Feb-06 2:51 
GeneralRe: Good job! Pin
flair5-Feb-06 14:54
flair5-Feb-06 14:54 
GeneralRe: Good job! Pin
Taiguo5-Feb-06 15:01
Taiguo5-Feb-06 15:01 
GeneralRe: Good job! Pin
Taiguo5-Feb-06 22:36
Taiguo5-Feb-06 22:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.