Click here to Skip to main content
15,897,371 members

[XML Serialization] Crashes application ?

Yvar Birx asked:

Open original thread
Hello Codeproject,

I am currently attempting to serialize a piece of data. But all my application is doing is crashing itself with:

"The type initializer for 'Terrasylum.Terrasylum' threw an exception."
No details are given, and the last thing I added was:

C#
// -- Create the Chunk.
/// <summary>
/// Creates the Chunk at the Cooardinate and returns the string..
/// </summary>
/// <param name="?"></param>
private string CreateChunk(int ChunkX, int ChunkY)
{
    // -- Create a stream writer.
    StreamWriter _Writer = new StreamWriter(Path + ChunkX + "_" + ChunkY + ".txt");

    // -- Create a new Chunk.
    Chunk _Chunk = new Chunk(ChunkX, ChunkY, ChunkSize, ChunkSize);

    // -- Generate the Chunk.
    for (int x = 0; x < ChunkSize; x++)
    {
        for (int y = 0; y < ChunkSize; y++)
        {
            // -- Block.
            byte Block = ID.BLOCK_AIR;
            if (ChunkY == 0 && y > ChunkSize / 2)
                Block = ID.BLOCK_GRASS;

            // -- Set the Block.
            _Chunk.SetBlockID(x, y, Block);
        }
    }

    // -- Get the XML string.
    string _ReturnString;
    _ReturnString = SerializeToString(_Chunk);

    // -- Write.
    _Writer.Write(_ReturnString);

    // -- Close the Writer.
    _Writer.Flush();
    _Writer.Close();

    // -- Return.
    return _ReturnString;
}


This is how I serialize:
C#
public static T SerializeFromString<T>(string xml)
{
    XmlSerializer serializer = new XmlSerializer(typeof(T));

    using (StringReader reader = new StringReader(xml))
    {
        return (T)serializer.Deserialize(reader);
    }
}


public static string SerializeToString(object obj)
{
    XmlSerializer serializer = new XmlSerializer(obj.GetType());

    using (StringWriter writer = new StringWriter())
    {
        serializer.Serialize(writer, obj);

        return writer.ToString();
    }
}


And these are my (Chunk and Block class):
C#
[Serializable]
public class Chunk
{
    // -- Blocks.
    private Block[,] _Blocks;

    // -- Cooardinates.
    public int X { get; set; }
    public int Y { get; set; }

    // -- Chunk.
    /// <summary>
    /// Initializes a new Chunk based on the Width and Height given.
    /// </summary>
    /// <param name="Width"></param>
    /// <param name="Height"></param>
    public Chunk(int X, int Y, int Width, int Height)
    {
        // -- Create the Block List.
        _Blocks = new Block[Width, Height];

        // -- Create all the Blocks.
        for (int i = 0; i < Width; i++)
        {
            for (int j = 0; j < Height; j++)
            {
                // -- Create a new block.
                Block _Block = new Block(ID.BLOCK_AIR);

                // -- Add the block.
                _Blocks[i,j] = _Block;
            }
        }
    }

    // -- Set a Block.
    /// <summary>
    /// Sets the block in the Chunk.
    /// </summary>
    /// <param name="X">X of the block.</param>
    /// <param name="Y">Y of the block.</param>
    /// <param name="ID">The ID of the block.</param>
    public void SetBlockID(int X, int Y, byte ID)
    {
        // -- Set the Block.
        _Blocks[X, Y].ID = ID;
    }

    // -- Get a block.
    /// <summary>
    /// Returns the ID of the given block.
    /// </summary>
    /// <param name="X">The X of the Block.</param>
    /// <param name="Y">The Y of the Block.</param>
    public byte GetBlockID(int X, int Y)
    {
        // -- Return.
        return _Blocks[X, Y].ID;
    }
}


C#
[Serializable]
public static class ID
{
    public static int BlockSize = 32;

    public const byte BLOCK_AIR = 30;
    public const byte BLOCK_DIRT = 31;
    public const byte BLOCK_GRASS = 32;
}

[Serializable]
public class Block
{
    public byte ID { get; set; }

    // -- Block
    /// <summary>
    /// Initializes a new Block with the ID given.
    /// </summary>
    /// <param name="_ID">The ID of the block.</param>
    public Block(byte _ID)
    {
        // -- Set the ID.
        ID = _ID;
    }
}



Could anyone help me out ? If I get this fixed I can finally move on. :(
Tags: C# (C# 2.0, C# 3.0, C# 4.0)

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900