none
How to imports/exports to xml using CodeFirst RRS feed

  • Question

  • Hi all,

    Currently working to develop a .NET app, I use "Code First" Entity Framework to access data from my database.

    I need to export data in XML, and to be able to import more from Xml. I should also thereafter be able to upgrade a database in runtime.

    I know there is a method "WriteXml" with the dataset, but, I am constrained to use "CodeFirst".

    Do you have a solution to this problem?

    Thank you for your help
    Thlac.

    PS: Some of the parameters of this problem are imposed for this project. They will therefore deal with:
    "Code First"
    The imports and exports in XML (at least) and CSV (for bonus)



    • Edited by thlac Friday, January 13, 2012 10:17 AM
    • Moved by Paul Zhou Monday, January 16, 2012 5:37 AM move for better support (From:.NET Base Class Library)
    Friday, January 13, 2012 9:40 AM

Answers

  • I believe the Xml and CodeFirst here are orthogonal unless you would like to store Xml in your database. Correct if I am wrong but I think you have an Xml file that contains data that can be turn into entities which you would like to save in the database e.g. you have the following Xml document:

    <People>
      <Person>
        <ID>1</ID>
        <FirstName>John</FirstName>
        <LastName>Smith</LastName>
      </Person>
    </People>
    


    And you would like to end up with a table called Person that has 3 columns ID, FirstName and LastName containg the corresponding values from the Xml. If this is the case then this is probably a two step process - in the first step you would turn the Xml into entities (in the case above you would have a Person object with properties set accordingly). Then, in the second step, you would just need to add the created objects to the context and SaveChanges.

    There several ways to turn xml into entities:
    - you could use XmlSerialization (XmlSerializer class and friends) which would do all the hard work for you. It's not a fastest thing in the world however and you may need to annotate properties of your entities with attributes and as a result they will can no longer be pure POCOs. Also there are some limitations to what can be deserialized. (You could also can take a look at DataContractSerializer - I think it can do more or less the same XmlSerializer does)
    - you could use Linq to Xml (or XmlReader) in which case you would create objects on your own and set properties of created entities accordingly - it is more work but this can be much faster and your entities will continue to be POCO as you won't need to decorate entities with any additional attributes

    With regards to serializaiton - it is again not really related to database and CodeFirst. CodeFirst/Entity Framework will give you entities/objects. Now you can do with them whatever you need to. If you want to serialize them that is fine. Again you can use either XmlSerialization (XmlSerializer et al.) or write your own thing with Linq to Xml or XmlWriter. As far as the serialization to CSV is concerned I am not aware of any class in the .NET Framework that does it but it should be pretty easy to write.

    Hope this helps.

    Pawel

     

    Tuesday, January 17, 2012 2:11 AM
  • Hi thlac,

    I'd like share a link with you here: http://social.msdn.microsoft.com/Forums/en/adodotnetentityframework/thread/2d6b75ee-a1b7-44bf-b5f3-d32b12f2db66

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, January 17, 2012 8:59 AM
    Moderator

All replies

  • I'm moving this thread to ADO.NET Entity Framework and LINQ to Entities forum to get better support.

    Have a nice day.


    Paul Zhou [MSFT]
    MSDN Community Support | Feedback to us
    Monday, January 16, 2012 5:37 AM
  • I believe the Xml and CodeFirst here are orthogonal unless you would like to store Xml in your database. Correct if I am wrong but I think you have an Xml file that contains data that can be turn into entities which you would like to save in the database e.g. you have the following Xml document:

    <People>
      <Person>
        <ID>1</ID>
        <FirstName>John</FirstName>
        <LastName>Smith</LastName>
      </Person>
    </People>
    


    And you would like to end up with a table called Person that has 3 columns ID, FirstName and LastName containg the corresponding values from the Xml. If this is the case then this is probably a two step process - in the first step you would turn the Xml into entities (in the case above you would have a Person object with properties set accordingly). Then, in the second step, you would just need to add the created objects to the context and SaveChanges.

    There several ways to turn xml into entities:
    - you could use XmlSerialization (XmlSerializer class and friends) which would do all the hard work for you. It's not a fastest thing in the world however and you may need to annotate properties of your entities with attributes and as a result they will can no longer be pure POCOs. Also there are some limitations to what can be deserialized. (You could also can take a look at DataContractSerializer - I think it can do more or less the same XmlSerializer does)
    - you could use Linq to Xml (or XmlReader) in which case you would create objects on your own and set properties of created entities accordingly - it is more work but this can be much faster and your entities will continue to be POCO as you won't need to decorate entities with any additional attributes

    With regards to serializaiton - it is again not really related to database and CodeFirst. CodeFirst/Entity Framework will give you entities/objects. Now you can do with them whatever you need to. If you want to serialize them that is fine. Again you can use either XmlSerialization (XmlSerializer et al.) or write your own thing with Linq to Xml or XmlWriter. As far as the serialization to CSV is concerned I am not aware of any class in the .NET Framework that does it but it should be pretty easy to write.

    Hope this helps.

    Pawel

     

    Tuesday, January 17, 2012 2:11 AM
  • Hi thlac,

    I'd like share a link with you here: http://social.msdn.microsoft.com/Forums/en/adodotnetentityframework/thread/2d6b75ee-a1b7-44bf-b5f3-d32b12f2db66

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, January 17, 2012 8:59 AM
    Moderator