How to XML serialize an array of Lists of objects?

前端 未结 3 1749
终归单人心
终归单人心 2021-01-20 02:53

I am trying to save an array of lists of an object in C# with in a xml file. I succeeded to save a array of an object and a list of objects but not an array of lists of an o

3条回答
  •  滥情空心
    2021-01-20 03:33

    Try converting the list to an array, before attempting to serialize it:

    List[] MainArr = new List[1];
    MainArr[0] = new List();
    Box Box1 = new Box(1);
    MainArr[0].Add(Box1);
    
    var arr = Array.ConvertAll(MainArr, x => x.ToArray());
    
    System.Xml.Serialization.XmlSerializer writer = new System.Xml.Serialization.XmlSerializer(arr.GetType());
    System.IO.StreamWriter fileWrite = new System.IO.StreamWriter(@"C:\Users\Giorgos\Desktop\ConsoleApplication1\ArrListBox.xml");
    writer.Serialize(fileWrite, arr);
    fileWrite.Close();
    

    The above piece of code produces the following xml on my machine:

        
    
      
        
          1
        
      
    
    

提交回复
热议问题