views:

208

answers:

2

Here is the code:

    [XmlRoot("Foo")]
    class Foo
    {
        [XmlElement("name")]
        string name;
    }

    [XmlRoot("FooContainer")]
    class FooContainer
    {
        [XmlElement("container")]
        List<List<Foo>> lst { get; set; }
    }

    XmlSerializer s = new XmlSerializer(typeof(FooContainer)); -->Can't pass through this.

Complains about not being able to implicitly cast it blah blah blah,

Anyone can tell what is wrong with this code?

+1  A: 

Foo and FooContainer need to be public. Other than that it worked fine for me. Had to flesh out the code a bit, but his works ...

class Program
{
    static void Main(string[] args)
    {
        XmlSerializer s = new XmlSerializer(typeof(FooContainer));

        var str = new StringWriter();
        var fc  = new FooContainer();

        var lst = new List<Foo>() { new Foo(), new Foo(), new Foo() };

        fc.lst.Add( lst );

        s.Serialize(str, fc);
    }
}

[XmlRoot("Foo")]    
public class Foo    {        
    [XmlElement("name")]        
    public string name = String.Empty;    }    

[XmlRoot("FooContainer")]    
public class FooContainer    {

    public List<List<Foo>> _lst = new List<List<Foo>>();
    public FooContainer()
    {

    }

    [XmlArrayItemAttribute()]
    public List<List<Foo>> lst { get { return _lst; } }
}
JP Alioto
I have them both public but still complaining?
theKing
AWESOME!!! It works just the way I expected it to work :)
theKing
A: 
Luke Schafer
The solution you provided works?
theKing
yeah it works. JP's is better though
Luke Schafer