3

I need to serialise data to XML but I am having real trouble working out how to do this. (in Visual Studio)

I need to create this type of XML seen below structure. But the Object FormType contains ILists and it wont serialize.

<?xml version="1.0" encoding="utf-16"?>
<VersionXml xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ImportId>1</ImportId>
<Environment>SIT</Environment>
<DateExported>12/2/2014</DateExported>
<FormType>
    <Id>4000</Id>
    <FormTypeVersion>
        <DisplayName>display name here</DisplayName>
        <FormNumber>12345<FormNumber>
        <Name>12345-abc<Name>
        <CompanyId>1</CompanyId>
        <Active>1<Active>
        <RevisionHistoryNumber>2<RevisionHistoryNumber>
    <FormTypeVersion>
    <FormTypeVersion>
        <DisplayName>display name here</DisplayName>
        <FormNumber>12345<FormNumber>
        <Name>12345-abc<Name>
        <CompanyId>1</CompanyId>
        <Active>1<Active>
        <RevisionHistoryNumber>3<RevisionHistoryNumber>
    <FormTypeVersion>
</FormType>
<FormType>
    <Id>4001</Id>
    <FormTypeVersion>
        <DisplayName>another one here</DisplayName>
        <FormNumber>456<FormNumber>
        <Name>456-bcd<Name>
        <CompanyId>1</CompanyId>
        <Active>1<Active>
        <RevisionHistoryNumber>3<RevisionHistoryNumber>
    <FormTypeVersion>
    <FormTypeVersion>
        <DisplayName>another one here</DisplayName>
        <FormNumber>456<FormNumber>
        <Name>456-bcd<Name>
        <CompanyId>1</CompanyId>
        <Active>1<Active>
        <RevisionHistoryNumber>1<RevisionHistoryNumber>
    <FormTypeVersion>
</FormType>
</VersionXml>

Here is my class I tried to create, but FormType will not serialize and gets a reflector error

[Serializable]
    public class FormXml
    {
        public string ImportID  { get; set; }
        public string Environment { get; set; }
        public string DateExported { get; set; }  
        public IEnumerable<FormType> FormType { get; set; }
    }

This is the error received:

Cannot serialize member FormXml.FormType of type System.Collections.Generic.IEnumerable`1..... because it is an interface.

I cannot change the IList to a List - so is there another way to do this?

Here is thr FormType.cs

[Serializable]
    public class FormType : Entity
    {
        public virtual ProductCode Product { get; set; }

        public virtual String DisplayName { get; set; }

        public virtual String FormNumber { get; set; }

        public virtual String Name { get; set; }

        public virtual Boolean Active { get; set; }

        private IList<FormTypeVersion> _versions = new List<FormTypeVersion>();

        public virtual IList<FormTypeVersion> Versions
        {
            get { return _versions; }
            set { _versions = value; }
        }
    }
user3437721
  • 2,227
  • 4
  • 31
  • 61

2 Answers2

2

to achieve this use a serializable type instead of IEnumerable<FormType>, maybe a List<FormType>?

[edit] Of course, FormType must implement ISerializable too.

Adrian Nasui
  • 1,054
  • 9
  • 10
1

So for resources that I have from you in question I came with example

Foo

[Serializable]
[XmlRoot("Foo")]
public class Foo
{
    [XmlArray("BarList"), XmlArrayItem(typeof(Bar), ElementName = "Bar")]
    public List<Bar> BarList { get; set; }
}

Bar

[Serializable]
public class Bar
{
    public string Property1 { get; set; }
    public string Property2 { get; set; }
}

Code to test

Foo f = new Foo();
f.BarList = new List<Bar>();
f.BarList.Add(new Bar() { Property1 = "s", Property2 = "2" });
f.BarList.Add(new Bar() { Property1 = "s", Property2 = "2" });

FileStream fs = new FileStream("c:\\test.xml", FileMode.OpenOrCreate);
System.Xml.Serialization.XmlSerializer s = new System.Xml.Serialization.XmlSerializer(typeof(Foo));
s.Serialize(fs, f);

Output

<?xml version="1.0" ?> 
<Foo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <BarList>
        <Bar>
            <Property1>s</Property1> 
            <Property2>2</Property2> 
        </Bar>
        <Bar>
            <Property1>s</Property1> 
            <Property2>2</Property2> 
        </Bar>
    </BarList>
</Foo>

this is showing how to serialize the xml with custom class lists.

You could also refer to:

Serializing Lists of Classes to XML

XML Serialize generic list of serializable objects

XML Serialization and Deserialization

EDIT: you could aslo:

[Serializable]
[XmlRoot]
public class FormXml
{
    public string ImportID  { get; set; }
    public string Environment { get; set; }
    public string DateExported { get; set; }  
    [XmlIgnore]
    public IEnumerable<FormType> FormType { get; set; }
    [XmlElement, Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
    public List<FormType> Foo { get { return FormType.ToList() } set { FormType = value; } }
}
Community
  • 1
  • 1
Sebastian 506563
  • 6,980
  • 3
  • 31
  • 56