Is there a way to make an object (with its attributes) serialize to xml?

Create a class (name it FormElement). This class must have some properties, such as the metadata it has with data items (name, ordinal, value that is only a string, etc.).

This class has the attributes of the Validation Application Block Validation classes.

I want to serialize it to xml and deserialize it. Make sure all properties of the class, including the validator application block attributes, are serialized.

any suggestion?

+1


source to share


4 answers


The .NET Framework has this functionality 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 deserializing an XML file back into 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 them to work. If you want to make your XML output a little prettier, you can use the [XmlElement] and [XmlAttribute] attributes on your class properties to serialize them into your selection schema.

+3


source


By serializing, do you mean using the official Serialization mechanism or achieving a similar effect?



If your objects are beans, you can use reflection to write a generic service that takes a class and records its name and class properties. It can also read stuff from XML and generate an object (which Apache Digester essentially does).

0


source


What Jonathan Holland said.

However, you also asked a validation question. If you use Jonathan's code all your properties will serialize and de-serialize correctly. But if you really want to check it, there is a property that you can set using the XmlSerializer object for the * .xsd schema to check. You can easily create a schematic from your class using the command line tool xsd.exe

that comes with Visual Studio.

Also, it looks like you can control whether certain properties of your class are serialized as attributes or elements. Jonathan touched on this, 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 each property that you want to serialize must also be serializable, and complex types must be serialized as XmlElements. Otherwise, you cannot do it.

0


source


XStream is a pretty good java library for this.

-1


source







All Articles