XML Deserialization is returning empty object?

F

Frank

Hi All,

I've been yanking my hair out all day over this one, and finally decided to
grab some sample code from MSDN, only to find the exact same problem

What's the problem? The problem is that the call to Deserialize( ... ) is
returning an empty object and I don't understand why?


oItem = (OrderedItem) serializer.Deserialize(reader); <= That line
returns empty order object

Here is the code from Microsoft, very easy to create C# console app for you
to try as well.
Thanks for any tips on this one!

Frank
===============================================================
using System;

using System.Collections.Generic;

using System.Text;

using System.IO;

using System.Text;

using System.Xml;

using System.Xml.Serialization;

// This is the class that will be deserialized.

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;

}

}


public class Test

{

public static void Main(string[] args)

{

Test t = new Test();

// Read a purchase order.

t.DeserializeObject("simple.xml");

}

private void DeserializeObject(string filename)

{

Console.WriteLine("Reading with XmlReader");

// Create an instance of the XmlSerializer specifying type and namespace.

XmlSerializer serializer = new XmlSerializer(typeof(OrderedItem));

// A FileStream is needed to read the XML document.


FileStream fs = new FileStream(filename, FileMode.Open);

XmlReader reader = new XmlTextReader(fs);


// Declare an object variable of the type to be deserialized.

OrderedItem oItem;

// Use the Deserialize method to restore the object's state.

oItem = (OrderedItem) serializer.Deserialize(reader);
//<=== Look at oItem values in debugger, they are all 0 or null ????

// Write out the properties of the object.

Console.Write(

oItem.ItemName + "\t" +

oItem.Description + "\t" +

oItem.UnitPrice + "\t" +

oItem.Quantity + "\t" +

oItem.LineTotal);

}

}

/* simple.xml XML FILE (make sure there's no whitespace at the top of the
file)



<?xml version="1.0"?>

<OrderedItem xmlns:inventory="http://www.cpandl.com"
xmlns:money="http://www.cohowinery.com">

<inventory:ItemName>Widget</inventory:ItemName>

<inventory:Description>Regular Widget</inventory:Description>

<money:UnitPrice>2.3</money:UnitPrice>

<inventory:Quantity>10</inventory:Quantity>

<money:LineTotal>23</money:LineTotal>

</OrderedItem>

*/
 
F

Frank

This simple.xml file works, after yanking out the namespaces...

I also changed the XmlReader to new and XmlTextReader (see previous posted
code)

So, what's the deal with namespaces? If you have more than one?


/* simple.xml XML FILE (make sure there's no whitespace at the top of the
file)
<?xml version="1.0"?>

<OrderedItem>

<ItemName>Widget</ItemName>

<Description>Regular Widget</Description>

<UnitPrice>2.3</UnitPrice>

<Quantity>10</Quantity>

<LineTotal>23</LineTotal>

</OrderedItem>

*/
 
J

John Saunders

Frank said:
This simple.xml file works, after yanking out the namespaces...

I also changed the XmlReader to new and XmlTextReader (see previous posted
code)

So, what's the deal with namespaces? If you have more than one?

Namespaces in XML are like namespaces in C#:

<foo:book xmlns:foo="urn:foo"/>

and

<foo:book xmlns:foo="urn:bar"/>

Are two totally unrelated things.

John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top