views:

15

answers:

1

Hi there.

I have the following test code for the main list:

public class ListBaseClass
{
    public int State { get; set; }
    public List<BaseClass> Items { get; set; }      
}
public class BaseClass
{
    public int ObjectVersion { get; set; }
    public int State { get; set; }
    ...
}

public class ChildClass_1: BaseClass
{
    public double Limit { get; set; } 
}

public class ChildClass_2: BaseClass
{
    public double Limit { get; set; } 
}

Then my code to deserialize my XML is like this:

private void button4_Click(object sender, EventArgs e)
{
        ListBaseClass data;

        using (FileStream fs = new FileStream(@".\MultiTestItemXML.xml", FileMode.Open))
        {
            XmlSerializer ser = new XmlSerializer(typeof(ListBaseClass));
            data = (ListBaseClass)ser.Deserialize(fs);
        }

        MessageBox.Show("number of objects " + data.Items.Count.ToString());
}

The messagebox results is zero - there are none there - but my xml have data. A sample of my XML file:

[ListBaseClass]

[State]1[/State] [Items] [ChildClass_1] [ObjectVersion]15[/ObjectVersion] [State]0[/State] [Limit]1[/Limit] [/ChildClass_1] [ChildClass_1] [ObjectVersion]15[/ObjectVersion] [State]0[/State] [Limit]3[/Limit] [/ChildClass_1] [ChildClass_1] [ObjectVersion]15[/ObjectVersion] [State]0[/State] [Limit]2[/Limit] [/ChildClass_1] [ChildClass_2] [ObjectVersion]15[/ObjectVersion] [State]0[/State] [Limit]2[/Limit] [/ChildClass_2] [ChildClass_2] [ObjectVersion]15[/ObjectVersion] [State]0[/State] [Limit]2[/Limit] [/ChildClass_2] [ChildClass_2] [ObjectVersion]15[/ObjectVersion] [State]0[/State] [Limit]2[/Limit] [/ChildClass_2] [/Items] [/ListBaseClass]

PS.: Its not accepting XML data here - so I change to [] - sorry for the mess.

If I change the List type to be one of the specific child, and remove the other child from the XML, then it works fine.

A: 

Hi

Can you replace all ChildClass_ with BaseClass in your xml document.

Reason behind this , your ListBaseClass is having a property List Items, so while deserilization process , Desilizer expect node to be present in the XML.

But if you need to retain this information so cretae a peoperty in the BaseClass named

public int ChildType which store information which type of the child (ChildClass_1 or ChildClass_2) is required.

try this approach.

saurabh