Monthly Archives: August 2013

Preserving Newlines in XML Serialization

With .NET’s XML serialization, it kills newlines when you serialize XML to disk. More specifically, it converts a CR+LF into just an LF (\r\n becomes just \n).

This was causing annoyances with the spell editor, since you can edit source code for spells with it, but the code would appear all on one line after saving and reloading.

The fix was to declare XmlWriterSettings with a newline preference.

The code was this:

         public void Save()
         {
             XmlSerializer serializer = new XmlSerializer(GetType());
             Stream stream = new FileStream(FileLocation.SpellDirectory + FileName,
                 FileMode.Create, FileAccess.Write, FileShare.None);
             serializer.Serialize(stream, this);
             stream.Close();
         } 

And now it’s this:

        public void Save()
        {
            XmlWriterSettings ws = new XmlWriterSettings();
            ws.NewLineHandling = NewLineHandling.Entitize;
            XmlSerializer serializer = new XmlSerializer(GetType());
            Stream stream = new FileStream(FileLocation.SpellDirectory + FileName,
                FileMode.Create, FileAccess.Write, FileShare.None);
            XmlWriter writer = XmlWriter.Create(stream, ws);
            serializer.Serialize(writer, this);
            stream.Close();
        }

I can have my newlines and eat them too. Oh joy!

I Am Addicted To Github

Until recently, the only code I’ve released as open source has been the Magma MUD codebase.

In the process of posting the Magma source on Github, I kind of got hooked on posting code online. Since then I’ve posted the source for a handful of applications, a mix of Linux and Windows desktop apps. A 12-day commit streak so far, yay!

I’m also considering open-sourcing the Basternae 3 codebase. The main things that make it easier to work with than old-timey C-based MUDs are the use of C#, which has amazing exception handling and debugging capabilities (no more attaching gdb to a core dump), and speaks XML natively, so data files are Human-readable, portable, fairly robust, and extensible. The editor is also getting to be pretty good.

To open source Bast3, I’d need to write a lot more documentation, and I’d need to “genericize” a lot of things that are specific to Basternae. It’d be a lot of work, but I think it’d be a fun project. The source is already in a private repository on Github, but that’s the easy part.