none
Can EF 4.0 do this for me? Link to XML possibly? RRS feed

  • Question

  • Currently have a system that takes XML and we created a schema for it.  We then take this schema and use the Schema Definition Tool (XSD), as mentioned here, and generate a set of C# classes.  Then these classes are used in the process to load with data at run time.

    Now Im familiar with EF and LINQ to Entites and LINQ to Objects, but im wondering if the above could be replaced by using LINQ to XML in some fashion? 

    Every so often the schema changes or we get some new additions to the xml, and we have to regenerate the classes.  Kind of a pain.

    Would be nice if we could simply load up objects without having to go through the hassle of rebuilding the classes from scratch when the xml structure changes.

    suggestions?

    Now that Iv'e typed the above and re-read it, I suspect LINK to XML is a viable option.  Can I still do schema validation at some point with LINQ to XML?

    Correction:

    The system I built about 2 years ago, used the above approach - the schema def tool to generate classes.  However, the xml structure hardly ever changed.

    The system I am looking to modify now, uses this approach currently.  However, the xml structure could change often, as in 1/month with new senders.

    • Edited by shiftbit Friday, March 1, 2013 2:57 PM
    Friday, March 1, 2013 2:17 PM

All replies

  • Hi Shiftbit,

    Welcome to the MSDN forum.

    I am not very clear what do you want to do via Entity Framework. Do you mean you want to use Entity Framework to generate class from xml?

    Entity Framework (EF) is an object-relational mapper that enables .NET developers to work with relational data using domain-specific objects.

    Best Regards,


    Alexander Sun [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, March 4, 2013 6:55 AM
  • Hi,

    Yes, LINQ to XML can validate an XML document based on a schema. http://msdn.microsoft.com/en-us/library/bb387037.aspx

    LINQ to XML might work for you, as long as you can write .NET code that will write the classes out. But it seems like T4 might be a better option. You should be able to read an XML file into a T4 template to generate the classes you need, a code first EF model could then create the tables for you if it connects to an empty database.

    I haven't tried out using T4 for something like this, but it is probably the first thing I would try out. Though the other idea you have might work as well.

    After I typed that out I thought that perhaps the idea of LINQ to XML was to move generation of the classes to runtime, which T4 wouldn't as much with I don't think.


    We are seeing a lot of great Entity Framework questions (and answers) from the community on Stack Overflow. As a result, our team is going to spend more time reading and answering questions posted on Stack Overflow. We would encourage you to post questions on Stack Overflow using the entity-framework tag. We will also continue to monitor the Entity Framework forum.

    Monday, March 4, 2013 5:50 PM
    Moderator
  • Well, Ive used the T4 templates with Entities, so this would be something I am familiar with.  However, what I am trying to determine is if I really need these intermediate classes at all.

    Using the schema tool to generate classes lets me populate instances of these classes at run time - and this lets me read object properties instead of a bunch of xpath statements to grab the data from the source xml.

    In other words, the end goal is when Im getting ready to load the final destination with data, I can reference object properties instead of xml node values.

    However, as I mentioned, I am re-thinking this approach, in favor of just pulling the data out of the xml at the point of use, and doing so with LINQ to XML.  So this lets me handle updates to xml structure changes without having to re-generate classes.

    Tuesday, March 5, 2013 4:58 PM