Question

Create a class (call it FormElement). That class should have some properties like the metadata they have with data elements (name, sequence number, value—which is just a string, etc).

This class has as attributes of type Validation Application Block Validation classes.

I want to serialize it to xml and deserialize it. Verify that all properties of the class including the validation application block attributes survive serialization.

some suggestion?

No correct solution

OTHER TIPS

The .NET framework has this built in, using C# you would do it like this:

// This code serializes a class instance to an XML file:
XmlSerializer xs = new XmlSerializer(typeof(objectToSerialize));

using (TextWriter writer = new StreamWriter(xmlFileName))
{
     xs.Serialize(writer, InstanceOfObjectToSerialize);
} 

And this snippet is an example of how to deserialize an XML file back to a class instance:

// this code creates a class instance from the file we just made:
objectToSerialize newObject;
XmlSerializer xs = new XmlSerializer(typeof(objectToSerialize));

using (TextReader reader = new StreamReader(xmlFileName))
{
    newObject = (ObjectToSerialize) xs.Deserialize(reader);
}

You must mark your class with the [Serializable] attribute for these to work. If you want to make your XML output a little more pretty, you can use [XmlElement] and [XmlAttribute] attributes on your class properties to have them serialize into your schema of choice.

By saying serialize, do you mean use the official Serialization mechanism, or achieve a similar effect?

If your objects are beans, you could use reflection to write a general service that takes a class and writes down its class name and properties. It can similarly read materials from the XML and generate the object (which is what Apache Digester essentially does).

What Jonathon Holland said.

However, you also asked about validation. If you use the code Jonathan posted, all of your properties will serialize and de-serialize correctly. But if you really want to check it, there is a property you can set with your XmlSerializer object for a *.xsd schema to validate against. You can create the schema easily enough from your class by using the xsd.exe command-line tool that is included with Visual Studio.

Also, it sounds like you might want to control whether certain properties of your class are serialized as attributes or elements. Jonathon touched on that, but I want to show an example:

[Serializable]
public class FormElement
{
   [XmlAttribute]
   public string Name {get; set;};

   [XmlAttribute]
   public int Sequence {get; set;};

   [XmlAttribute]
   public string Value {get; set;};

   [XmlElement]
   public Validation OnValidate{get; set;}

   [NonSerialized]
   public string UnimportantProperty {get; set;};

}

Finally, the type for every property that you want to serialize must also be serializable, and complicated types must be serialized as XmlElements. Otherwise you won't be able to do it.

XStream is a pretty good java library for doing just that.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow