I am Working on a project (C#) in the university and they said that we can't use a DBMS like SQL Server so we decide to use Linq and XML...we learned some basics in Linq to Xml But really we don't know how we can create tables and fields and work with them in Xml.any suggestions ?
The simplest option would be a Typed Dataset saved to an XML file. (With or without LINQ)
You would use it just like an RDBMS, but it isn't an RDMS, so it should be allowed.
If it's a single user application you can just create a serializable class and use that to store your data. Then when your app is closed the data class is serialized (binarily) to a file, and when the app starts it reads the file and all your data is still there.
Here is a simple example from the docs.
If you really want to use tables, you can create a Typed DataSet and Save/Load it as (proprietary) XML.
A DataSet can hold multiple tables + relations between them.
Related
I'm a novice programmer. I am full of theoretical knowledge, but I'm behind with the practice. OK. I am trying to make a program for adding categories and descriptions to files. The language is C#, it should run on Windows 7...
1.The categories can contain sub-categories.
I don't want to call them "tags", because these are different. A category can be fx "favorites". But it can also be: "favorites->music->2013". You can create sub-categories, I will use a TreeView on a WinForm for all the operations a user can do with them.
QUESTION: Should I use XML file for the categories?
2.Every file CAN have a description and one or many categories. However:
Even if the file is deleted, I want to keep its description, so that it can be available for later usage.
Folders themselves will be omitted. The folders themselves cannot have nor categories, nor description. But the contained files YES.
I made a very simple SQL Server database containing one table: !http://img832.imageshack.us/img832/3931/finalprojectdb.png
QUESTION: Is this a good idea? Maybe the categories column is better to be of type XML ?
Any advice on what should the best approach in this situation be, is welcomed. Thanks in advance !
SQL is not great for getting nested data at once. You can store things in XML which gives you a lot of flexibility, but you also have to write a parser or deserializer for it. Nowadays people also just write a little Javascript class and use something like Newtonsoft to deserialize it automatically.
If you want a DB solution, you can use something like SQLite embedded in your application if you don't want to install a database separately.
XML is a great design for an app that needs to communicate cross platform (say c# to java), or cross internet, or cross network. But as a way to store data as a subset in a table, not really.
A normalized database is a terrific tool. It can be indexed (xml can not) this allows for rapid querying of data. If you de-normalize your data by embedding xml in a column querying it will be slow and updating / maintaining a pain.
I personally prefer foreign key tables.
Is it possible to automatically convert a lot of C# classes to SQL Server database tables ?
I need to import some xml files into SQL Server but using SSIS the runtime stops working as soon as I select the xsd file. However I easily converted the xsd to class files so that's why I am asking if its possible to have a workaround...
You can use Entity Framework Code First functionality, with a little extra work to get the tables done in your database.
There you have a good post on it by ScottGu
Yes, it's possible to automatically do this. Entity Framework has a model called Code First, which essentially does this: takes your C# classes and creates the database and tables automatically for you.
Take a look at this post by Scott Guthrie.
Other option you might test is DataSet.ReadXml() function. Drawback is the Dataset can't handle complexType="mixed", but it deals well with large files (my files had about 50M each). All tables and columns are named by XML tags and relations are autogenerated by DataSet itself.
I have a C# WPF program that needs to display GridView and 2D graph data which initially will come from a hardware device. I also want to continuously (or frequently periodic) backup this data to an XML file on disk. What would be the easiest way to implement this in visual studio? Should I create an XML schema first, or use the dataset designer? Should I bother with datasets at all or would it make sense to eliminate them and write my incoming data directly to xml?
I would recommend:
Plan a structure of an XML ahead. Create a simple empty file to help you along the way.
Create a data serialization provider as well as the interface that it will implement. In your case it will be an XML provider (who knows, you may need to save the data to a database in future. You should plan ahead for that.)
Write a custom class that serializes your poco domain objects into an xml using LinqToXML.
I have a C# data structure which consists of classes and collections which have references to each other. Currently I am using Serialization to store the structure in XML, which works as a handy way to save/load the structure into my application.
If I want to be able to save/load the structure into a database, is there a simple way? Should I use LINQ?
Just to be 100% clear LINQ has nothing to do with storing data in a database. LINQ is a language query for c# with syntax that resembles SQL.
So in order to answer your question you could look at the Entity Framework (I'd recommend this if you are using .NET 4.0) or LINQ to SQL
It currently depends on your needs, and the db engine you are using:
If you need to perform queries against the XML contents you can use a XML field (mssql and oracle do support them).
If you only need to store/retrieve it, just store it in a long string field (NText in sqlserver. NCLob in Oracle).
Have you tried NHibernate?
EDIT: I don't know if this would be ok for you, but if you can define DB tables to hold your data structures, you could have code auto-generated using LINQ to SQL. Check ScottGu's tutorial on LINQ to SQL. If your data structures are not changing too often this could be a good way to go.
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.