none
Entity Framework, XSD, XML loading data into entities RRS feed

  • Question

  • I am looking for a quick and easy solution to a problem that I have.

    Basically I have a Web API that accepts XML as input. I have created an XSD for this XML and converted it into VB Classes using the XSD.EXE program.

    I can read the XML data and populate the VB Classes using an XMLSerializer and a memorystream to populate the classes.

    Now I am looking to store this object built from the classes into the database using EF, or DataSets, or well anything else.

    Was hoping for a nice simple solution as per loading the XML into the classes using XMLSerializer.

    Thanks

     

    Monday, September 16, 2013 3:04 PM

Answers

  • Hi,

    Yes, I think it is a good idea.

    If we know the entity properties, I recommend to create the entity first.

    Regards.


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    • Marked as answer by gilesrpa Wednesday, September 18, 2013 7:55 AM
    Wednesday, September 18, 2013 3:59 AM
    Moderator

All replies

  • Hello gilesrpa,

    Thanks for visiting this forum.

    As far as I know, entity framework does not provide function like XDS.EXE to load XML to entities.

    For achieving what you want, we need to write some codes.

    Here I made a sample and please see it below.

    Let us assume that the xml have been serialized to be below:

    <?xml version="1.0"?>
    <catalog>
       <book id="bk101">
          <author>Gambardella, Matthew</author>
          <title>XML Developer's Guide</title>
          <genre>Computer</genre>
          <price>44.95</price>
          <publish_date>2000-10-01</publish_date>
          <description>An in-depth look at creating applications 
          with XML.</description>
       </book>
       <book id="bk102">
          <author>Ralls, Kim</author>
          <title>Midnight Rain</title>
          <genre>Fantasy</genre>
          <price>5.95</price>
          <publish_date>2000-12-16</publish_date>
          <description>A former architect battles corporate zombies, 
          an evil sorceress, and her own childhood to become queen 
          of the world.</description>
       </book>
       <book id="bk103">
          <author>Corets, Eva</author>
          <title>Maeve Ascendant</title>
          <genre>Fantasy</genre>
          <price>5.95</price>
          <publish_date>2000-11-17</publish_date>
          <description>After the collapse of a nanotechnology 
          society in England, the young survivors lay the 
          foundation for a new society.</description>
       </book>
       <book id="bk104">
          <author>Corets, Eva</author>
          <title>Oberon's Legacy</title>
          <genre>Fantasy</genre>
          <price>5.95</price>
          <publish_date>2001-03-10</publish_date>
          <description>In post-apocalypse England, the mysterious 
          agent known only as Oberon helps to create a new life 
          for the inhabitants of London. Sequel to Maeve 
          Ascendant.</description>
       </book>
       <book id="bk105">
          <author>Corets, Eva</author>
          <title>The Sundered Grail</title>
          <genre>Fantasy</genre>
          <price>5.95</price>
          <publish_date>2001-09-10</publish_date>
          <description>The two daughters of Maeve, half-sisters, 
          battle one another for control of England. Sequel to 
          Oberon's Legacy.</description>
       </book>
      </catalog>

    And let's say suppose you've got the same structure (an empty datatable in your db, and you've mapped it in the EF's file).

    var result = from e in XDocument.Load("xxx.xml").Descedants("book")
    
                     select new
    
                     {
    
                         BookId = e.Attribute("id").Value,
    
                         Author = e.Element("author").Value,
    
                         …………………… 
    
                     };
    foreach(var item in result)
    
    {
    
           //Suppose your EF's model's name is Book, then——
    
          Book book = new Book{BookId=item.BookId,BookAuthor=item.Author,……};
    
          XXXEntities.AddToBook(book);
    
    }
    
    XXXEntities.SaveChanges();
    

    I do not whether it is simple for you. But we do need to use the XSD.EXE, isn’t it?

    Have a try.

    Thanks&Regards.


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Tuesday, September 17, 2013 1:42 AM
    Moderator
  • Thank you for the quick reply, after looking around the net I was coming to the same conclusion myself.

    I have a very complex data structure which contains many classes to create a single object from the XML. From this I have found that the quickest way was to deserialize the xml from a stream directly into a set of classes that had been generated by the XSD.exe tool.

    Try to convert from VB to C#

    _obj       //Object created from classes build from xsd.exe

    XDocument _xDoc = XDocument.Load("filename");

    XmlSerializer _serial = new XmlSerializer(TypeOf(_obj);

    MemoryStream _mem = new MemoryStream();

    StreamWriter _rdr = new StreamWriter(_mem);

    _rdr.writer(_xDoc);

    _rdr.Flush();

    _mem.position = 0;

    _obj = _serial.deseralize(_mem)

    The above code will transform the XML direct into the XSD built classes.

    I was just wondering if there was a stream method for creating an EF or Dataset object directly.

    Therefore, I suppose the best way would be to use Linq to XML to pull the data and then manually create the EF objects from that?

    Tuesday, September 17, 2013 8:13 AM
  • Hi,

    Yes, I think it is a good idea.

    If we know the entity properties, I recommend to create the entity first.

    Regards.


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    • Marked as answer by gilesrpa Wednesday, September 18, 2013 7:55 AM
    Wednesday, September 18, 2013 3:59 AM
    Moderator