Problem with xml serialisation

I have a problem with xml serialisation. I tried to run the following code : (this is an example from the microsoft website)

public class OrderedItem

{

public string ItemName;

public string Description;

public decimal UnitPrice;

public int Quantity;

public decimal LineTotal;

// A custom method used to calculate price per item.

public void Calculate()

{

LineTotal = UnitPrice * Quantity;

}

}

class test

{

static void Main(string[] args)

{

run.testrelationsort();

Console.WriteLine("Writing With Stream");

XmlSerializer serializer =

new XmlSerializer(typeof(OrderedItem));

OrderedItem i = new OrderedItem();

i.ItemName = "Widget";

i.Description = "Regular Widget";

i.Quantity = 10;

i.UnitPrice = (decimal)2.30;

i.Calculate();

// Create a FileStream to write with.

Stream writer = new FileStream("Test.xls", FileMode.Create);

// Serialize the object, and close the TextWriter

serializer.Serialize(writer, i);

writer.Close();

}

}

serializer.Serialize(writer, i); gives an error : There was an error generating the XML document.

when look insite the exeption, I see following innererror : Could not load type 'OrderedItem' from assembly 'Test, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.":"Questionaire.OrderedItem

I use microsoft visual c# express edition.

Does anyone know what goes wrong here.

 




Answer this question

Problem with xml serialisation

  • jonnybravo

    The posted code works fine on my machine.  Are you splitting this up into separate assemblies
  • DonDoThat

    You need to decorate the OrderedItem class with the [Serializable] attribute like so:

    [Serializable]
    public class OrderedItem
    {
    ...

     



  • Problem with xml serialisation