以下是代码:
[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.
抱怨无法隐瞒它等等等等,
任何人都可以说出这段代码有什么问题?
答案 0 :(得分:2)
Foo和FooContainer需要公开。除此之外它对我来说很好。不得不充实代码,但他的作品......
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; } }
}
答案 1 :(得分:1)
我知道有人会提到公众,所以我会跳到这里:
是的,他们需要公开,但这不是唯一的问题。实际上运行序列化不起作用(得到描述的错误)
它不喜欢List
[XmlRoot("Foo")]
public class Foo
{
[XmlElement("name")]
public string name;
}
[XmlRoot("FooContainer")]
public class FooContainer
{
[XmlElement("container")]
public List<SerializableList<Foo>> lst { get; set; }
}
[XmlRoot("list")]
public class SerializableList<T>
{
[XmlElement("items")]
public List<T> lst { get; set; }
}