none
ICollection generic list to XML RRS feed

  • Question

  • places help

            public int ID{ get; set; }

            public string name{ get; set; }
            public ICollection<price> price{ get; set; }

    like generic list convert 

             var serializer = new XmlSerializer(productList.GetType());

    problem.

    help me places

    thank you

    Wednesday, October 11, 2017 7:36 PM

All replies

  • Hello gezginyasin,

    First you should  pay attention to interface property.interface cannot be serialized directly. Because the object type is unknown until runtime.For your situation,the most easily way is that you use list generic type rather than ICollection generic type.Please try the below code and tell me if it works well.

    //I'm not sure what your class structure. I create a structure as below.

    public class ProductList { public int ID { get; set; } public string name { get; set; } public List<price> price { get; set; } } public class price { public double pr { get; set; } } class Program { static void Main(string[] args) { ProductList productList = new ProductList { ID = 1, name = "P1", price = new List<price>{ new price { pr = 1.0 }, new price { pr = 2.0 }, new price { pr = 3.0 } } }; XmlSerializer xmlSerializer = new XmlSerializer(productList.GetType()); using (StreamWriter streamWriter = File.CreateText("T1.txt")) { xmlSerializer.Serialize(streamWriter, productList); } } }


    And if you just want to stay with interfaces you should done much job with it,there are some options you could choose.

    Sincerely,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, October 12, 2017 9:17 AM
    Moderator
  • Hello gezginyasin,

    Is there anything updated? If the issues has been solved, you could close the thread by marking the reply as answer, This can be beneficial to other community members reading this thread.

    Sincerely,
    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Sunday, October 29, 2017 8:19 AM
    Moderator