Map relational database to XML in C# - c#

I have an application that needs to map relational database to XML.
The issue is that the database structure is given (I cannot change it) and the XML Schema is given (comes from WSDL).
I need to let the user of my application to tell the system that a given column in a table is mapped to a given element in the schema. Then my application needs to crate XMLs based on the database data.
Is there a technology that can help me? is there a C# control for that.
Thanks

XSLT.
I think you should dynamically (based on user input) create xslt and transform data from database (dataset?) to xml.

What's your database - SQL Server? You can achieve a lot of XML flexibility with the SELECT .... FOR XML statement in SQL Server (most definitely in 2005 and up).
And if that's not flexible enough, then I'd second "peeles" recommendation - do a SELECT...FOR XML first and transform the output using XSLT.
Marc

Select...For sounds simpler; but LINQ in your C# is probably another option.

Related

Where to migrate data from SQL Server?

I'm working on an application which was previously developed with EF, SQL Server, C# and WinForms. For some reasons we need to remove SQL Server and use some and put data in some other container like XML, Excel sheet or in memory classes.
DAL was designed using EF with repository and UOW pattern. Can you please suggest me what can be the best way to migrate and what format will be easy to transfer data?
Since the data access is decoupled into a repository you should be able to write a new repository instance that will allow you to retrieve your data in its new format. Specifically if you wanted to hold the data in XML you could just write a new repository that uses Linq-to-XML to retrieve your data.
I don't know of a good quick way to transfer your data from SQL Server into XML. If I were doing it I'd probably write some custom application to make the conversion, but I suspect there are better solutions out there for this type of migration.
Your question duplicates this questions:
Entity Framework with XML Files
How to use Entity Framework 4.0 with Xml or in-memory Storage (non-SQL)
There seems to be a provider for virtuoso xml:
http://virtuoso.openlinksw.com/dataspace/dav/wiki/Main/VirtAdoNet35Provider
Some hints can be found here:
http://social.msdn.microsoft.com/Forums/en/adodotnetentityframework/thread/6f6164f6-fced-43de-b3fc-eccbd74dd482
http://blogs.msdn.com/b/adonet/archive/2009/11/05/model-first-with-the-entity-framework-4.aspx
http://en.wikipedia.org/wiki/ADO.NET_Entity_Framework
EF ist basically developed for database abstraction. You will need to write your own xml database provider if you try to strore XML instead.
Your options are:
move to SQL compact (but not in XML)
use XPath and rewrite your code
use Xpath for your own database provider
Be aware that your xml file size and speed depends on the amount of data! Using multiple xml files can end up in a mess.

Need to load multiple sql server tables from C# app

Hi I have a pretty complex schema (.xsd) and an .xml files. Some nodes have about 70 attributes and some 10-15. I used xsd2code to create c# classes and then deserialized my xml file. I created a database based on my .xsd class using xsd2db application. So I've read my xml file and need to store it into the database. I have 74 classes/tables with primary/foreign key relationships. I looked into SqlBulkCopy - for that it looks like I need to convert everything into datatables/rows/columns. And I will need to take into account the hierarchy for satisfying foreign keys. Is there another way of doing it? What would be the best (fastest) approach?
Thank you
Jenny
There are different ways to import a XML file either through T-SQL, SSIS, VB Script, etc.
Sorry to just google and post links, but I think sql squirrel has found the easiest method for getting an XML file into SQL Server 2005 is to use T-SQL:
http://www.lockergnome.com/sqlsquirrel/2008/05/22/how-to-import-a-xml-file-into-sql-server-2005/
http://www.lockergnome.com/sqlsquirrel/2008/05/23/how-to-turn-imported-xml-into-a-relational-format-in-sql-server-2005/
The SQLTeam seem to think T-SQL is the WTG as well:
http://weblogs.sqlteam.com/mladenp/archive/2007/06/18/60235.aspx

Representing relational database data as XML and using XPath to get results

we have a relational database with some data and we need to offer the content of the database via XML web services. We also have to enable users to get parts of the XML representation using XPath (also later there may be a need to modify data indirectly using XML representation of the data with XQuery). Is there a simple way to achieve this in C#? The user will be known XML schema of the data.
Keywords for web search also appreciated.
Read up on OData ;) A lot better than bad web services. Full tooling support in .NET (LINQ integrated) it is a semantical way to expose data via intelligent web services.
There are a few ways I discovered, although I couldn't find much data.
One is to use an SQL query which returns an XML, the other is to use DataSet class with some of it XML processing possibilities. DataSet also supports XPath queries.

Store Relational database as XML

I have to store relational data (text, numbers and pictures) into an XML file. I want to maintain the relationship between data and the tables structure.
How do I go about doing that?
My Windows Mobile application use Windows Communication Foundation (WCF) to get data. Once it gets the data it stores them into an XML file.
The WCF use ADO.NET Entity Framework to data retrieve.
I'm going to use SQL Server 2008.
The data will be use to show it on application. I don't want to use SQL Server CE to store data because it's slower. Maybe, instead of using XML file I can use text plain files to store texts and numbers, and image files to store images.
In short, XML will be a way to store the data instead using SQL Server CE or WCF.
Thank you!
Thanks for the additional detail.
My suggestion is first, try SQL Server CE. It might not actually be slower for what you're doing. The time you save in not reinventing the wheel might be better spent in speeding up other parts of your application.
For the rest, assuming you don't need to store the data for long, nor do much with it, I'd suggest copying the data from the Web Service return type into a DataSet. The DataSet type has a WriteXml method that can save the data, and a ReadXml method that can read it back in.
While it's a little ambiguous what is needed, you can try looking into 'FOR XML EXPLICIT' under SQL Server. This will include relationships. Link: http://www.eggheadcafe.com/articles/20030804.asp
what about a dataset ? define the tables and relations, load the data into the dataset, and save it as XML (WriteXml method)
Define your XML format as an XSD. If you use Visual Studio use add new DataSet and the "custom tool" will automagically create a strong-typed set of classes based on DataSet. You can define tables and relations with constraints. It is easy to do in Visual Studio. If you are Visual Studio averse, you can create the XSD by hand and use xsd.exe to generate your code.
Once you have the strong-typed DataSet filled with data you can serialize it to an XML stream by calling WriteXml.
What do you mean by, "maintain the relationship between data and the tables structure"? Are you talking about more than maintaining the ID of a source database record in an attribute of an XML node? What do you need to do with the XML data once you have it? Will it be edited and have to be re-applied to the database?
More questions than answers from me....sorry.
What SQL Server are you using? Some servers allow you to request an XML response for a query. That could save you a lot of work.
EDIT: Since you have specified that you will use MSSQL 2008, you may be in luck. In MSSQL 2005 and later there is a "FOR XML" keyword which allows you to get results as XML. Give it a try.

What is the best way to generate XML from the data in the database?

If I have thousands of hierarchical records to take from database and generate xml, what will be the best way to do it with a good performance and less CPU utilization?
You can output XML directly from SQL Server 2005 using
FOR XML
The results of a query
are returned as an XML document. Must be used with
one of the three RAW, AUTO
and EXPLICIT options
RAW
Each row in the result set is an XML element with a generic
identifier as the element tag
AUTO
Results returned in a simple
nested XML tree. An element will
be generated for each table field in the
SELECT clause
EXPLICIT
Specifies the shape of the resulting
XML tree explicitly.
A query must be written in a
particular way so that additional
information about the nesting is
specified
XMLDATA
Returns the schema, but does not add the root element to the result
ELEMENTS
Specifies that the columns are
returned as child elements to the table
element. If not specified, they are mapped as
attributes
Generate an inline XSD schema at the same time using
XMLSCHEMA
You can handle null values in records using
XSINIL
You can also return data in Binary form.
You might want to have a look on MSDN for XML support in SQL Server 2005, for technologies such as XQuery, XML data type, etc.
That depends - if your application and database servers are on separate machines, then you need to specify which CPU you want to reduce the load on. If your database is already loaded up, you might be better off doing the XML transform on your application server, otherwise go and ahead and use SQL Server FOR XML capabilities.
Oracle has tools for that, so I guess SQL-Server does too, but you'll need a schema. Personally for small set I use a php script I have around, but for big stuff with need for customization is another story.

Categories