views:

263

answers:

5

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?

A: 

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).

Uri
A: 

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

Cogsy
He is looking for a c# solution.
pug
Six months ago when he asked the question, he hadn't specified a language...
Cogsy
A: 

I use .net framework with C#.

+3  A: 

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.

FlySwat
I would change typeof(objectToSerialize) to typeof(ClassToSerialize) or objectToSerialize.GetType(). As it stands, it would be a bit confusing for those new to .NET
Richard Szalay
A: 

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.

Joel Coehoorn
Your code is misleading. The [Serializable] and [NonSerialized] attributes have nothing to do with Xml Serialization!! I think you want [XmlIgnore] on the UnimportantProperty, and you want nothing (no attribute at all) on the class. Or if you want to be explicit, [XmlType].
Cheeso