Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Export to XML from Dynamic Data Site

I have to update an existing site that's based on DDS on top of the Entity Framework, and it uses three different database models from three different databases. And what it needs is a simple addition to the ListDetails page: an export to XML button...
Adding the button is easy. Creating the XML isn't difficult either. The challenge is getting the proper table to export, walk through all records and fields and generate the XML based on any of the 100+ tables in the system!
So, my button calls an export (ashx) handler which generates the XML based on the table who'se name it receives through it's parameters. The code I have is something like this:

        Content_CobaEntities Coba = new Content_CobaEntities();
        MetadataWorkspace metadataWorkspace = Coba.MetadataWorkspace;
        EntityContainer container = metadataWorkspace.GetItems<EntityContainer>(DataSpace.CSpace).First();
        string namespaceName = metadataWorkspace.GetItems<EntityType>(DataSpace.CSpace).First().NamespaceName;
        EntitySetBase entitySetBase = container.BaseEntitySets.FirstOrDefault(set => set.ElementType.Name == Entity);

Unfortunately, this only works with a single context and I happen to have three. (But this one is the main context.) (Besides, a second parameter could be used to determine the proper context.) And although it allows me to determine the entity type that's needed, it still won't provide me access to it's records and fields.

So, how to I get the data for this entity? (Filters aren't important; the export will return all data.)

And no, no EF4. No .NET 4. This is an older VS2008 project and cannot be modified to much, nor can it be upgraded...


Basically, the querystring contains two parameters: ContextID and QueryID. The ContextID tells me which context to use and since I only have three different contexts, a simple switch-command solves this for me. But one of the contexts contains 60+ queries, each of them related to a single database table. The Dynamic Data Site provides me with options to add, edit and delete records from this table but it needs to be exported in a format dictated by my custom code. But with 60 tables, it's too much to write a switch statement for each and every query so I need something more generic.

like image 360
Wim ten Brink Avatar asked Jun 27 '11 07:06

Wim ten Brink


People also ask

How do I export an XML database?

Click File > Save As, and select the location where you want to save the file. , point to the arrow next to Save As, and then click Other Formats. In the File name box, type a name for the XML data file. In the Save as type list, click XML Data, and click Save.

Can XML Share Data?

Since XML data is stored in plain text format, it provides a software- and hardware-independent method of sharing data. This makes it much easier to create data that different applications can use. It also makes it easier to expand or upgrade a system to new operating systems, servers, applications, and browsers.


1 Answers

As it turns out, I made things more complex than needed... I start with the parameters:

        string Entity = context.Request.QueryString.Get("Entity");
        string ContextID = context.Request.QueryString.Get("Context");

Then I need to determine the right context to use, which is easy:

        ObjectContext Context;
        if (ContextID.Contains("Content_LogEntities")) { Context = new Content_LogEntities(); }
        else if (ContextID.Contains("Content_CobusEntities")) { Context = new Content_CobusEntities(); }
        else { Context = new Content_CobaEntities(); };

Next I need to get the proper data in the requested table. Also easy:

        ObjectQuery Tabel = Context.CreateQuery<EntityObject>(string.Format("[{0}]", Entity));

Don't know why I was trying to make that part more complex than needed.

What's left is walking through all fields, but I have a reflection work-around:

        foreach (var rec in Tabel)
        {
            foreach (PropertyInfo Prop in rec.GetType().GetProperties())
            {
                // Blah
            }
        }

The Blah-part checks the Prop.Name To see if it's an entity key, entity state, child collection or reference or if it's just a data-field. This allows me to generate a practical XML output.

like image 62
Wim ten Brink Avatar answered Oct 05 '22 23:10

Wim ten Brink