Tools for debugging / checking XML Serialization [closed] Tools for debugging / checking XML Serialization [closed] xml xml

Tools for debugging / checking XML Serialization [closed]


For those viewing this question, I have found that adding event handlers for XmlSerializer's UnknownNode and UnknownAttribute events is very helpful. Even if you just leave it throwing a new NotImplementedException, you can set a breakpoint and see when unknown nodes and attributes are encountered.

For example:

public void Open(string filename){    // Create serializer    XmlSerializer serializer = new XmlSerializer(typeof(ObjectType));    // Set event handlers for unknown nodes/attributes    serializer.UnknownNode += new XmlNodeEventHandler(serializer_UnknownNode);    serializer.UnknownAttribute += new  XmlAttributeEventHandler(serializer_UnknownAttribute);    // ...}private static void serializer_UnknownAttribute(object sender, XmlAttributeEventArgs e){    throw new System.NotImplementedException();}private static void serializer_UnknownNode(object sender, XmlNodeEventArgs e){    throw new System.NotImplementedException();}


The simplest way to test these type of problems (where serialization is incomplete or incorrect) is to unit test - nothing complicated.

  • Create an object of your serializable type
  • Set all of the properties
  • Serialize it
  • Take the serialized output and deserialize it into a new object
  • Check all of the properties of the object to make sure they're still populated
  • Fail the unit test if any of the properties aren't set to the expected value

Remember that it's usually the behaviour you're trying to prove - not the implementation. Tools that check for specific attributes are only of value for testing a single implementation of your code: a unit test like the above could work for any form of serialization or storage without rewriting the test.


What do you mean "an item". If a type is internal, you should see an error message. The outermost exception isn't usually very helpful, but trace down through .InnerException to the bottom and it usually spells out exactly what the problem is.

If a member is entirely internal, then sure - it will be skipped.

IMO, unit /integration tests are your real friend here - the desired output from serialization is ultimately outside the compiler, so it doesn't matter whether you get a compile-time message if the output doesn't match what you expect. What I mean here is: do the serialization and compare to an expected output file. Ditto input.

For example, trying to serialize:

[XmlRoot("Node", Namespace="http://flibble")]public class MyType {    [XmlElement("chileNode")]    public string Value { get; internal set; }}

gives (at runtime):

Unable to generate a temporary class (result=1). error CS0200: Property or indexer 'MyType.Value' cannot be assigned to -- it is read only

which is pretty specific.