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>
<inventoryescription>Regular Widget</inventoryescription>
<money:UnitPrice>2.3</money:UnitPrice>
<inventory:Quantity>10</inventory:Quantity>
<money:LineTotal>23</money:LineTotal>
</OrderedItem>
*/
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>
<inventoryescription>Regular Widget</inventoryescription>
<money:UnitPrice>2.3</money:UnitPrice>
<inventory:Quantity>10</inventory:Quantity>
<money:LineTotal>23</money:LineTotal>
</OrderedItem>
*/