How to create xml file from a List<T> object? RRS feed

  • Question

  • User-353733005 posted


    I want to create a xml file from a List<T> object. Can anyone help me please.

    Thanks in advance.

    Wednesday, February 4, 2015 5:25 AM


All replies

  • User-760709272 posted

    You could create an XML structure\file programmatically using XmlWriter or XmlDocument, or you could use XML Serialisation to create the document for you from your list.



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, February 4, 2015 6:11 AM
  • Wednesday, February 4, 2015 6:59 AM
  • User-353733005 posted

    Hi AidyF,

    Your solution really help me a lot but now I faced another problem which is I create the List<T> using entity framework so when the xml serialization convert the list data it show that "InvalidOperationException" because it's want the class in public but I've use entity framework for create the database object and get the database data.So I don't understand how to do that. My list look like this List<User> usr = new List<User>();

    Can you help me please.

    Thanks in advance.

    Thursday, February 5, 2015 12:22 PM
  • User-760709272 posted

    If you want certain properties to be ignored by serialization you normally decorate them with an attribute like


    if that is possible or not with your EF classes depends on how you are using EF really.  You could always look at custom serialization but that's going to be more work


    however it gives you complete control over the process.

    Thursday, February 5, 2015 1:30 PM