Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tools for debugging / checking XML Serialization [closed]

Are there any tools out there for helping to debug / check the xml serialization process?

For instance, suppose an item is marked as internal instead of public. There is no compile time error message, nor a runtime error message. If you set a breakpoint and step into the serialization process, the item is just just skipped. In other words, it is often hard to find these types of problems. A debug tool would allow you to step through the process and provide some feedback e.g. encountered this attribute, iterated through properties and didn't find a corresponding public one, skipped. Another option would be a check tool to examine all the classes with xml serialization attributes to make sure they are accessible and have set methods, etc.

like image 515
Tim Avatar asked Feb 25 '10 23:02

Tim


People also ask

What is XML serialization?

XML serialization is the process of converting XML data from its representation in the XQuery and XPath data model, which is the hierarchical format it has in a Db2® database, to the serialized string format that it has in an application.

What is Sgen EXE?

XML Serializer Generator Tool. This creates an xml serialization assembly for types in a specified assembly in order to improve the startup performance of a xmlserializer when it serializes or deserializes objects of the specified types. These assemblies can then be deployed with your application.

What is XML serialization and Deserialization?

Serialization is a process by which an object's state is transformed in some serial data format, such as XML or binary format. Deserialization, on the other hand, is used to convert the byte of data, such as XML or binary data, to object type.

What is XmlSerializer C#?

The XmlSerializer enables you to control how objects are encoded into XML, it has a number of constructors. If you use any of the constructors other than the one that takes a type then a new temporary assembly is created EVERY TIME you create a serializer, rather than only once.


2 Answers

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();
}
like image 141
Tim Avatar answered Oct 14 '22 05:10

Tim


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.

like image 25
Dan Puzey Avatar answered Oct 14 '22 06:10

Dan Puzey