locked
Entity Framework and Xml file RRS feed

  • Question

  • Hello,

    Context :
    I would like to get information from many servers and to gather them to a database to make statistics on it. Servers are indepndant and cannot access database directly.

    Idea : 2 steps
    - generate a xml file from each server
    - load these xml files into database

    Solution :
    1/ design my database
    2/ generate EF from it
    3/ use this object model to create xml files
    4/ use this object model to load xml files into db

    Questions:
    - It doesn't seem to be possible to connect EF to xml file, it is surprising.. When I do AcceptChanges, I would like to write to my xml file on server and vice versa, how ?
    - Better is to use dataset serialization ?

    Thank you.


    A.L
    Thursday, November 26, 2009 5:08 PM

Answers

  • Hello A.L,

     

    Welcome to ADO.NET Entity Framework and LINQ to Entities forum!

     

    For such a scenario, I have an idea to combine the LINQ to Entities and the LINQ to XML.  Because Entity Data Model is an object-oriented model, we can convert the XML file from the server into several entities.  Then we can create corresponding data tables in the database to map these entities.   Currently, in EF v1 (.NET 3.5 SP1), we need to generate the Entity Data Model from database.   So we can first design such a database structure to represent the XML file.   In EF 4.0 (.NET 4.0), we have a new feature called Model First which allows us to first build our Entity Data Model.  Then EF can generate the DDL to create the database for us.   For detail, please see http://blogs.msdn.com/adonet/archive/2009/11/05/model-first-with-the-entity-framework-4.aspx. 

     

    After the Entity Data Model and the database created, the task to connect the database can be done by LINQ to Entities, and the task to connect the in-memory entities and XML files can be done by LINQ to XML.  For some sample codes, I strongly recommend you the examples CSLinqToXML (C#) and  VBLinqToXML (VB.NET) of All-In-One Code Framework.  Please pay attention to the “Database XML methods” parts of the two samples.  We demonstrate how to combine the LINQ to SQL and LINQ to XML to generate XML files from database and then query the XML files.  The logic should be similar if we use LINQ to Entities instead.  

     

     

    If you have any questions, please feel free to let me know.

     

    Have a great day!

     

     

    Best Regards,
    Lingzhi Sun

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Friday, November 27, 2009 1:22 AM
  • Hello Adrien,

     

    For LINQ to XML that I mentioned in my last post, you can find more references here, http://msdn.microsoft.com/en-us/library/bb387098.aspx.   It allows us to use LINQ technologies on XML parsing, modifying, and etc. 

     

    First, I recommend you design the database structure according to the server data that you want to store.  Then the EF Entity Data Model can be created based on the data structure in the database.   The EF generated classes can be used as a series of in-memory objects to hold the data from the servers.   Then LINQ to XML can be used when creating the data from servers into XML files and read the XML files into EF entities.  Once we load the data from XML to the EF entities, LINQ to Entities can help us to store the data into the database correctly. 

     

    If it is not clear to you, could you please provide me with more detailed information of what kind of data on the server that you want to save?  I will do my best to figure out some sample codes for your references.

     

    Have a nice day!

     

     

    Best Regards,
    Lingzhi Sun

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Monday, November 30, 2009 5:11 AM

All replies

  • Hello A.L,

     

    Welcome to ADO.NET Entity Framework and LINQ to Entities forum!

     

    For such a scenario, I have an idea to combine the LINQ to Entities and the LINQ to XML.  Because Entity Data Model is an object-oriented model, we can convert the XML file from the server into several entities.  Then we can create corresponding data tables in the database to map these entities.   Currently, in EF v1 (.NET 3.5 SP1), we need to generate the Entity Data Model from database.   So we can first design such a database structure to represent the XML file.   In EF 4.0 (.NET 4.0), we have a new feature called Model First which allows us to first build our Entity Data Model.  Then EF can generate the DDL to create the database for us.   For detail, please see http://blogs.msdn.com/adonet/archive/2009/11/05/model-first-with-the-entity-framework-4.aspx. 

     

    After the Entity Data Model and the database created, the task to connect the database can be done by LINQ to Entities, and the task to connect the in-memory entities and XML files can be done by LINQ to XML.  For some sample codes, I strongly recommend you the examples CSLinqToXML (C#) and  VBLinqToXML (VB.NET) of All-In-One Code Framework.  Please pay attention to the “Database XML methods” parts of the two samples.  We demonstrate how to combine the LINQ to SQL and LINQ to XML to generate XML files from database and then query the XML files.  The logic should be similar if we use LINQ to Entities instead.  

     

     

    If you have any questions, please feel free to let me know.

     

    Have a great day!

     

     

    Best Regards,
    Lingzhi Sun

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Friday, November 27, 2009 1:22 AM
  • Hello Lingzhi,

    Thank you very much for your full answer.

    I will prefer .Net 3.5 rather than 4.0.

    You said that I can use EF but for my first app on servers (memory objects-->xml serializer), I won't have sql connection...therefore no EF possible ??, what I want to do is taking advantage of EF to store data into entities and to serialize it into xml. My second app (xml->sql loader) will use these entities to load xml files into database.

    What I really want to look at is an equivalent (if exists) of "dataset like" solution when you create a xsd schema to generate wrapper classes then to fill objects and finally to create xml file. What is the equivalent in EF, LINQ To xxx technologies ?

    Thank You

    Adrien


    A.L
    Friday, November 27, 2009 3:32 PM
  • Hello Adrien,

     

    For LINQ to XML that I mentioned in my last post, you can find more references here, http://msdn.microsoft.com/en-us/library/bb387098.aspx.   It allows us to use LINQ technologies on XML parsing, modifying, and etc. 

     

    First, I recommend you design the database structure according to the server data that you want to store.  Then the EF Entity Data Model can be created based on the data structure in the database.   The EF generated classes can be used as a series of in-memory objects to hold the data from the servers.   Then LINQ to XML can be used when creating the data from servers into XML files and read the XML files into EF entities.  Once we load the data from XML to the EF entities, LINQ to Entities can help us to store the data into the database correctly. 

     

    If it is not clear to you, could you please provide me with more detailed information of what kind of data on the server that you want to save?  I will do my best to figure out some sample codes for your references.

     

    Have a nice day!

     

     

    Best Regards,
    Lingzhi Sun

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Monday, November 30, 2009 5:11 AM
  • Hello Adrien,

     

    How is the problem?  If you need any further assistance, please feel free to tell me.

     

    Have a nice day!

     

    Best Regards,
    Lingzhi Sun

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Thursday, December 3, 2009 12:46 AM
  •          

    What's wrong with my codes? Help please!

    here is my codes:   

          public string XMLSelect(int ParentID)
             {
                 XElement el = new XElement("Rows", new XAttribute("ParentID",ParentID),
                     from o in db.BoreholeDiameter
                     where o.Borehole.BoreholeID == ParentID
                     orderby o.DepthTo
                     select new XElement("row", new XAttribute("id",o.BoreholeDiameterID),
                         new XElement("cell", o.DepthTo),
                         new XElement("cell", o.diameter))
                   );
                 return el.ToString();
             }

    using Linq to Entity.

    error message:

    Only parameterless constructors and initializers are supported in LINQ to Entities.

    说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

    异常详细信息: System.NotSupportedException: Only parameterless constructors and initializers are supported in LINQ to Entities.

    源错误:

    行 899:         {
    行 900:             //在XML中加了一个 ParentID 属性
    行 901:             XElement el = new XElement("Rows", new XAttribute("ParentID",ParentID),
    行 902:                 from o in db.BoreholeDiameter
    行 903:                 where o.Borehole.BoreholeID == ParentID

     

    thanks!

    Monday, March 29, 2010 3:03 PM