0

I'm trying to deserialize an object back from it's XML string using xmlSerializer.Deserialize() but the returned object is always blank (not null, but all the properties are null or 0). I can't work out what I'm doing wrong and yet I get no errors or exceptions.

string xml = "***my xml is here***";

XmlSerializer ser = new XmlSerializer(typeof(Order));
StringReader stringReader = new StringReader(xml);
XmlTextReader xmlReader = new XmlTextReader(stringReader);
Order order = (Order)ser.Deserialize(xmlReader);
xmlReader.Close();
stringReader.Close();

The source of Order.cs was generated from the XSD using the the xsd.exe tool.

Source of order.cs: http://www.nickgilbert.com/etc/1/Order.txt

Sample order XML: http://www.nickgilbert.com/etc/1/example-order.xml

14
  • I was going to paste the XML and source of Order.cs but I can't see how to attach files on here. Commented Oct 10, 2011 at 11:27
  • Can you post your Order class? Commented Oct 10, 2011 at 11:28
  • 2
    Just place the code in your question. But you shouldn't include the whole source, just a minimal code that has the same problem. Commented Oct 10, 2011 at 11:28
  • 1
    Don't attach files, copy/paste the relevant parts into the question. Commented Oct 10, 2011 at 11:29
  • It appears correct to me. What does the the XML look like? Do the properties have getters/setters? How are you serializing Order? Are you putting Serializable tags on your properties? Commented Oct 10, 2011 at 11:30

2 Answers 2

2

Your sample XML file (example-order.xml) uses the namespace http://tempuri.org/OrderSchema.xsd but the code generated by XSD (order.cs) defines all of the elements in the namespace http://x-rm.com/wrightcottrell/cataloguecd/.

You'll need these namespaces to match up in order for serialization to work properly.

Sign up to request clarification or add additional context in comments.

Comments

1

The fact that you get an object back at all tells me that the object is public and has a public parameterless constructor (otherwise an exception would have been thrown). So, it is most-likely failing one of:

  • deserialization members must be either public properties with public get and public set, or public (non-readonly) fields
  • by default the member-names must be an exact match for xml element names, in the same xml-namespace as the parent element; finer control can be obtained via attributes (changing the name, using attributes, namespaces, etc)

2 Comments

I don't have a constructor for Order - I presume the values are populated by the Deserialize function rather than by the constructor?
@Nick yes you do; if you don't declare one, a public parameterless constructor is assumed by the compiler. Without a public parameterless constructor, it would not work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.