How to store XML files in SQL database using C#? - c#

I have an XML file which should be a part of a table in my SQL database (say, name, data and XML).
I prefer not to make a string of it if possible.
What column type should I use for it? I chose XML but I'm not sure how to work with it.
A small code example would be very appreciated.
Thanks.

SQL Server (if you're using it) has an XML datatype which you can use. I've never used the datatype but we covered it during an SQL Server/.NET course at university.
http://msdn.microsoft.com/en-us/library/ms189887.aspx

Related

dump XML into MySQL

I've got a database dump in XML coming from some Windows application. I believe its SQL Server 2008 data dumped into XML via C#, although I do not have access to the source. I only have the dump.
The XML file matches this format:
MSDN DataTable::WriteXmlSchema
Is there a known / convenient way to import this into MySQL? Is there anything in VisualStudio or MySQL Workbench that might help?
MySQL (as of version 5.5) has a LOAD XML syntax, see this link: https://dev.mysql.com/doc/refman/5.5/en/load-xml.html
If your data doesn't fit that schema, you might be able to transform it into that schema.
Otherwise, you could use the ODBC .NET Managed Provider to talk to MySQL from C# (http://support.microsoft.com/kb/310988), although I don't know whether it has been tested, or whether it is supported for MySQL.
From there you could parse the XML and create insert statements, etc.
Or, if you can't get the ODBC provider working, you could write a program to parse the XML, and write the appropriate SQL statements to insert all the data.
In short, you get to have fun with this.

Is it possible to put the XML content in a field of the database?

I was analyzing my situation and I face with the problem that I need to save the content of an XML file in SQL SERVER 2008. My XML files have a size around 200KB to 600KB
For the momment, how would I define the field to accept this content? I can imagine I can set the content directly but I'm not sue about this problem.
Thanks in advance.
SQL Server has a datatype called XML. Use that.
SQL Server has an xml data type exactly for that purpose. For example:
create table YourTable (id int identity, FileContent xml)
SQL 2008 supports a dedicated xml data type, which is documented here.
Some details include the following for fragments, and documents, respectively:
Restricts the xml instance to be a well-formed XML fragment. The XML
data can contain multiple zero or more elements at the top level. Text
nodes are also allowed at the top level.
Restricts the xml instance to be a well-formed XML document. The XML
data must have one and only one root element. Text nodes are not
allowed at the top level.
Also, a point on capacity (though a 2GB limitation should be hard to reach in the majority of cases):
The stored representation of xml data type instances cannot exceed 2
gigabytes (GB) in size. For more information, see Implementing XML in
SQL Server.
If you're working with SQL Server 2005+ you can use the xml data type. You'll need to be sure that your data is actually well-formed XML, though. If it's just snippets you're better off using nvarchar.
File sizes of 200 - 600 KB are no issue for the xml column type.
Did you try using BLOB data types or CLOB ?
SQLServer has an XML datatype.. have a look here
Yes, you can store XML text in a text field in your SQL database, but if you're planning to do a lot of introspection on that XML data, you'll probably be better off using MS-SQL's XML data type. This stores the XML data in tokenized form on the server and makes it possible to perform XML query operations without having to reparse the XML data all the time. I seem to recall you can also index on XML expressions as well.

Approach to process huge xml files in C#

Can someone please guide me with this problem?
In my institution, we process xml files of huge size(max 1 GB) and insert the details into a database table. Per current design, we are parsing xml file with XmlReader and form a xml string with required data, which will then be passed into a stored procedure (xml data type) to insert the details into db.
Now the problem is we are not sure if there would be a better approach other than this ? so please suggest if are any new features available with .Net 3.5 and/or sql server 2005 to handle this in a way better than our approach.
Any help in this reagrd would be highly appreciated.
Thanks.
Do you care at all what is in the XML-file? If not, you can just use a StreamReader and get the text from the XML and just pass it along to the database.
If you need to validate that the XML is correct, it is a good idea to use XmlReader.
However, just dumping 1GB of XML into your database seems a bit weird, what is the purpose of this XML data? Is it a lot of nested elements? Maybe you could de-serialize it and store each object in the appropriet table instead, which would imo lead to a easier understandable design.
There are a couple of things you can think of to make the design of your software easier/better:
Does more than one XML file occure in the database at once?
How is the data shared between applications?
Have you considered using MemoryMappedFile?
Is it possible to de-serialize the XML into entities instead and store them approprietly?
I suspect that if there are any performance issues it will be with the stored procedure and the database side of things rather that reading the file.
Why are you storing the XML file in a database table? I would suggest using a different solution would be appropriate, but without knowing more details about exactly what it is you are trying to do it is hard to advise.
If each first-level element in the xml is a record, i.e.
<rootNode>
<row>...</row>
<row>...</row>
<row>...</row>
</rootNode>
Then you could create an IDataReader implemention that reads the xml (via XmlReader) and presents each as a record, to be imported using SqlBulkCopy. Pretty much like my old answer here.
Advantages:
SqlBulkCopy is the fastest way to get data into a database
stripping it into records makes appropriate use of a database, allowing indexing and proper typing
it doesn't rely on a huge BLOB going over the wire in an atomic way (necessary for the xml data type)

EntityFramework and XML

We have a application that is making requests to a MSSQL DB via Entity Framework. One column in one table is a serialized C# class, stored in XML format. We would like to perform simple XPath queries against this data from C#. Currently we simply load the dataset, parse the XML via linq-to-XML then query the structure. This is of course the absolute worst way to solve this problem, so I'm looking for alternatives.
MS SQL 2008 can query XML data, so how do I do that through EF?
You can use a stored procedure. Or you can run dynamic SQL using ObjectQuery. Other than that, I don't know of any way to use the XML features of SQL Server through EF.

Convert a c# mdb solution to xml?

I have a system that uses an mdb database with an xsd descriptor written in c#. Now I want to use one or more xml files with the same data instead. I have generated a couple of adapters for the mdb, but now I don't know what is needed for using xml instead. Anyone have some tips? I have managed to save the mdb as a few xml files.
Very unclear, XML is a very poor substitute for a database. I reckon you'll want to use DataTable or DataSet to load the .mdb data. Their WriteXml() method makes it very easy to generate the xml.
The XML is not fully substitute for relation database. The dataadapters are not supposed to work with XML files and the SQL language too. I recommend you choose another SQL database (you need propably some embeded database - such as Firebird, PostreSQL, SQLite, MSSQL CE, etc...). You can still use a OLE DB data providers (DataAdapters, DataReaders, etc...) and the data layer will need only little change because of SQL dialects.
However, if you need the data in XML, you need change whole data access layer.

Categories

Resources