For those that are unfamiliar, from July 2003 until March 2011 this site ran under a custom PHP Content Management System I named bbXP, an acronym for Bulletin Board eXPerience. This project was one of the most influential projects I ever undertook in my free time and really defined the next ten years of programming career. So why the sudden desire to go back to something custom in lieu of WordPress? A simple answer: I love to do things from scratch and as practice for getting my ASP.NET MVC skills up to my ASP.NET WebForms skills. That answer leads to this blog series in which I'll be documenting my transition from this WordPress site to an ASP.NET MVC 4 Web Application and eventually a Windows Phone 8 native application when the MVC app is completed. In this blog post I will be reviewing the initial transition from a MySQL/WordPress installation to a baseline MVC application. A lot of people might disagree with my approach here, especially if you're coming at this from a DBA background. I prefer to do my data translation in C# instead of via SQL via DTS or some other SQL to SQL approach. If you're looking for that approach, I am sure there are other blog posts detailing that process. For those still reading, Step 1 in my mind in migrating to a new platform is getting the data out of WordPress. Luckily in WordPress you have an easy XML export from the Admin Menu: [caption id="attachment_1974" align="aligncenter" width="300"]Step 1 - Export Posts <span classin WordPress" width="300" height="156" class="size-medium wp-image-1974" /> Step 1 - Export Posts in WordPress[/caption] Depending on the amount of posts you have this could be anywhere between a few kb to a couple mbs. Step 2: Getting a strongly typed interface to the newly exported XML. Luckily there is a very awesome tool that has come with Visual Studio since at least 2008: xsd. The xsd tool can be run from it's location in the Visual Studio folder or it can be used anywhere via the Developer Command Prompt: [caption id="attachment_1976" align="aligncenter" width="300"]Step 2: Visual Studio 2012 Developer Prompt Step 2: Visual Studio 2012 Developer Prompt[/caption] Navigate to where you downloaded the WordPress XML Export and run the following two lines, assuming the name of your file was export.xml: [bash] xsd export.xml /d xsd export.xsd /d [/bash] Like so: [caption id="attachment_1977" align="aligncenter" width="300"]Step 3 - Generate Strongly Typed DataSet Step 3 - Generate Strongly Typed DataSet[/caption] After issuing those two commands you'll have a C# Class among a few other files (as seen in the screenshot below) to include in your C# Importer Application - a much better situation to be in when dealing with XML files or data migration I've found. [caption id="attachment_1978" align="aligncenter" width="300"]xsd tool generated Files xsd tool generated Files[/caption] Step 3 - Creating your new database schema. Now that you've got a clean interface for your XML file, you need to create a new database schema for your .NET Application. You could simply recreate it based on the structure of the XML file, but I took the more traditional approach of creating a normalized database schema in SQL Server: [caption id="attachment_1980" align="aligncenter" width="193"]Step 3 - Create your <span classnew database" width="193" height="198" class="size-full wp-image-1980" /> Step 3 - Create your new database[/caption] I'm not going to go over Database Schema Design, I feel it is very subjective and can vary from project to project. For those curious I do follow a pattern for keeping as little in each Table and instead create tables to reuse among other main tables (normalization). For instance with Tags, rather than tying a Tag to a specific Post, I created a relational table so many Posts can reference the same Tag. I did the same with Categories. Step 4 - Create your C# Importer App. Now that you've got your database schema it is time to create your Entity Model and write your C# code to actually import the XML file and populate your new SQL Tables. Pretty standard code for those that have used Typed DataSets and the EntityFramework - if you have questions please comment below and I'll be happy to help.
NewDataSet ds = new NewDataSet(); ds.ReadXml("export.xml"); using (var eFactory = new bbxp_jarredcapellmanEntities()) {
     foreach (NewDataSet.itemRow item in ds.item.Rows) {
     var creator = eFactory.Users.FirstOrDefault(a => a.Username == item.creator); if (creator == null) {
     creator = eFactory.Users.Create(); creator.Active = true; creator.Created = DateTime.Now; creator.Modified = DateTime.Now; creator.Username = item.creator; eFactory.Users.Add(creator); eFactory.SaveChanges(); }
var post = eFactory.Posts.Create(); post.Active = true; post.Created = DateTime.Parse(item.post_date); post.Modified = post.Created; post.Body = item.encoded; post.Title = item.title; post.PostedByUserID = creator.ID; eFactory.Posts.Add(post); eFactory.SaveChanges(); }
After running the importer and some MVC work later: [caption id="attachment_1982" align="aligncenter" width="300"]End Result of an Initial Conversion End Result of an Initial Conversion[/caption] More to come in the coming days, but hopefully that can help get someone pointed in the right direction on moving onto their new custom .NET solution. I should note in this initial migration I am not importing the tags, categories or comments - that will come in the next post.