473,799 Members | 2,907 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

XML Deserialization is returning empty object?

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.Dese rialize(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.Collecti ons.Generic;

using System.Text;

using System.IO;

using System.Text;

using System.Xml;

using System.Xml.Seri alization;

// 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.DeserializeOb ject("simple.xm l");

}

private void DeserializeObje ct(string filename)

{

Console.WriteLi ne("Reading with XmlReader");

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

XmlSerializer serializer = new XmlSerializer(t ypeof(OrderedIt em));

// A FileStream is needed to read the XML document.
FileStream fs = new FileStream(file name, FileMode.Open);

XmlReader reader = new XmlTextReader(f s);
// 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.Dese rialize(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.Descripti on + "\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="ht tp://www.cohowinery. com">

<inventory:Item Name>Widget</inventory:ItemN ame>

<inventory:Desc ription>Regular Widget</inventory:Descr iption>

<money:UnitPric e>2.3</money:UnitPrice >

<inventory:Quan tity>10</inventory:Quant ity>

<money:LineTota l>23</money:LineTotal >

</OrderedItem>

*/


Mar 8 '07 #1
2 4705
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>Widge t</ItemName>

<Description>Re gular Widget</Description>

<UnitPrice>2. 3</UnitPrice>

<Quantity>10</Quantity>

<LineTotal>23 </LineTotal>

</OrderedItem>

*/
Mar 8 '07 #2
"Frank" <Fr***@yahoo.co mwrote in message
news:45******** **************@ roadrunner.com. ..
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
Mar 8 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
5333
by: Eli Sidwell | last post by:
Trying to return a Recordset to an ASP and the Recordset is empty. The StorredProc works in the query analyzer and it even works from a quick VB app that I wrote to test it. The storedproc that I am using is fairly complex (creates some temporary tables and populates them with 'Insert Into Select ...', but the during testing the only Select statements that return visible rows is the final one that returns the finished table with an...
5
3111
by: Gent | last post by:
I have two questions which are very similar: Is it possible to return an object in C++. Below is part of my code for reference however I am more concerned about the concept. It seems like the function below is returning a pointer to pointers who are GUID. I am trying to write a wrapper to use in my VB code and what I would prefer to do is be able to return an array of GUID. I remember (not sure) that the concept of arrays does not really...
2
3825
by: Snowman | last post by:
Suppose I have a RootObject which holds a collection of other objects. The other objects have a property (Parent) which refers back to the "parent" collection (b.t.w. my collection is based on CollectionBase), in similar fashion as the object models of MS Office. I want to serialize this object graph (with RootObject as the xml document element) without Parent property serialized, this may be done by adding XmlIgnoreAttribute on the...
0
1120
by: Omars via DotNetMonster.com | last post by:
I am Deserializaing an XMl stream (C#). Everything works fine until I get empty tags into my Enums. For example if I have this field: Public EnumField myfield; Then I have: Public enum EnumField{ Type1,
3
2070
by: AnkitAsDeveloper [Ankit] | last post by:
Hi i am serializing a 'ref struct' object as follows : private: void Seri( String ^path, Object^ obj ) { FileStream^ fileStrm ; try { //Serialize entire object into Binary stream
3
9796
by: parrot toes | last post by:
Summary: I have been trying to make requests of a web service provided by Axis using a dotnet client with code generated by wsdl.exe and have been getting exceptions when trying to process the response. As a result of seraching news groups I guessed that the SOAP response defines an array element in a way that causes the dotnet deserialization routines to put the content in a generic object array (object) BUT the content is supposed to...
1
3752
by: parrot toes | last post by:
I tried to post this question before, but there was an error when posting. I case it did get posted and in order to avoid duplication, I'll just repost a summary. I have written a dotnet client that accesses a Axis provided web service. The client uses the code generated, using wsdl.exe, from the wsdl file provided by the web service provider. The web response has the following schema (roughly):
5
2294
by: Greg Allen | last post by:
I am consuming a web service and using the generated Reference.cs to access the service and the objects associated with it. I have run into a problem where some inherited classes are not being deserialized. I have verified that the XML being returned by the service contains the tags I am expecting, but they don't show up in the resulting object. Here's the relevant portion of the Reference.cs file: public class FormSection {
5
8640
by: Frank Hauptlorenz | last post by:
Hello, I recognized some days ago, that returning a DataTable blocks my WCF-Service. Is this a known bug? If I add this table to a new DataSet() and return this, it works. Thank you, Frank
0
9685
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10473
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10219
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9068
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7563
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6804
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5461
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5584
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2937
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.