1

I hope to find a solution from you. What I need is to serialize ValidatorList class object into an xml document. How to do this?

I tried like this:

XmlSerializer _serializer = new XmlSerializer(list); 

But I don't know how to make output of xml for list that has several classes.

C#

_list= new ListVal();
 Type _type = typeof(ras);

 _list.Add(new RequiredField
                    {
                      Property = _type.GetProperty("CustRef")
                    }.Add(new AsciiVal()));
_list.Add(new RequiredField
                  {
                    Property = _type.GetProperty("ctr")
                  }.Add(new StringLengthVal
                          {
                            min= 3,
                            max= 3
                          }));

[Serializable]
 public class Field
 {
public Field Next
{
  get;
  set;
}

public Field TypeName
{
  get;
  set;
}

    public Field PropertyName
  {
  get;
  set;
}
}

public class RequiredField : Field
{
//TODO
 }

public class AsciiVal: Field
 {
//TODO
  }

public class StringLengthVal: Field
 {
//TODO
}

 public class ListVal: List<Field>
 {
//TODO
  }
John Saunders
  • 160,644
  • 26
  • 247
  • 397
user235973457
  • 331
  • 4
  • 9
  • 24
  • FYI the process of turning an object into a stream of data (such as Xml) is *serialization*. *deserialization* is going the other way. Ive updated your question with the right phrases to make it clearer – Jamiec Jul 09 '13 at 10:18

3 Answers3

1

I have something close, but not exactly the Xml you want. In actual fact I think you'll see that the Xml produced below makes a bit more sense than what you have.

To get you started, you control the serialization and deserialization using attributes in the System.Xml.Serialization namespace. A few useful ones to read up on are

So I mocked up some code which closely matches your own. Notice the addition of some attributes to instruct the serializer how I want the Xml to be laid out.

[XmlInclude(typeof(AsciiValidator))]
[XmlInclude(typeof(RequiredValidator))]
[XmlInclude(typeof(StringLengthValidator))]
public class FieldValidator
{
    [XmlElement("Next")]
    public FieldValidator Next
    {
    get;
    set;
    }

    [XmlElement("PropertyName")]
    public string PropertyName
    {
    get;
    set;
    }
}

public class AsciiValidator: FieldValidator
{
}

public class RequiredValidator: FieldValidator
{
}

public class StringLengthValidator: FieldValidator
{
    [XmlElement]
    public int MinLength{get;set;}
    [XmlElement]
    public int MaxLength{get;set;}
}

[XmlRoot("ValidatorList")]
public class ValidatorList : List<FieldValidator>
{    
}

Point of interest; Every class inheriting FieldValidator must be added to the list of known types using XmlIncludeAttribute so the serializer knows what to do with them)

Then I created an example object map:

var test = new ValidatorList();
test.Add(
            new RequiredValidator()
                {
                    PropertyName="CustRef",
                    Next = new AsciiValidator()
                });            
test.Add(
            new RequiredValidator()
                {
                    PropertyName="CurrencyIndicator",
                        Next = new StringLengthValidator(){
                            MinLength=3,
                            MaxLength = 10
                        }
                });

Finally I told the serializer to serialize it (and output the result to the console)

var ser = new XmlSerializer(typeof(ValidatorList));
ser.Serialize(Console.Out,test);

This was the result:

<?xml version="1.0" encoding="utf-8"?>
<ValidatorList xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <FieldValidator xsi:type="RequiredValidator">
    <Next xsi:type="AsciiValidator" />
    <PropertyName>CustRef</PropertyName>
  </FieldValidator>
  <FieldValidator xsi:type="RequiredValidator">
    <Next xsi:type="StringLengthValidator">
      <MinLength>3</MinLength>
      <MaxLength>10</MaxLength>
    </Next>
    <PropertyName>CurrencyIndicator</PropertyName>
  </FieldValidator>
</ValidatorList>

Not a million miles away from what you wanted. There is the need to output certain things in a certain way (eg xsi:type tells the serializer how to deserialize back to the object map). I hope this gives you a good start.

Here is a live, working example: http://rextester.com/OXPOB95358

Deserialization can be done by calling the Deserialize method on the XmlSerializer.

For example, if your xml is in a string:

var ser = new XmlSerializer(typeof(ValidatorList));
var test = "<..../>" // Your Xml
var xmlReader = XmlReader.Create(new StringReader(test));
var validatorList = (ValidatorList)ser.Deserialize(xmlReader);

There are many overrides of Deserialize which take differing inputs depending if the data is in a Stream an existing reader, or saved to a file.

Jamiec
  • 133,658
  • 13
  • 134
  • 193
  • By calling one of the `Deserialize` methods on [`XmlSerializer`](http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializer.aspx) – Jamiec Jul 09 '13 at 11:34
  • i have tried it - it is not working. i would like to see yours what is like so that i can understand. thanks so much for ur time :) – user235973457 Jul 09 '13 at 11:52
0

You have to decorate the class that contains the _validators field with the KonwnType attribute

[Serializable]
[KwownType(typeof(RequiredFieldValidator)]
[KwownType(typeof(AsciValidator)]
public class MySerialisableClass
Rene Niediek
  • 147
  • 9
0

I have several SO answers that detail how to serialize objects using XML. I'll provide links below.

However, since you're looking for a rather simple serialization of your object, you may want to read up on the DataContractSerializer. It's much less complicated than the old .NET 1.x XML Serialization.

Here's the list of SO answers:

Even though many of these have to do with XML serialization and namespaces, they contain complete examples of serializing an object to XML using .NET 1.x XML Serialization.

Community
  • 1
  • 1
fourpastmidnight
  • 4,032
  • 1
  • 35
  • 48