I'd like to know what is the best practice.
I'm exporting some data (tables) from my database to XML files so I can read my data from them.
To export, I'm using DataTable.WriteXml (c#)
Now to read, what is best? to import them into a dataset and use DataTable.Select to get the row I want or create an XPathDocument and use XPath to get the data? which performs best? I'm still learning xpath.
Why not load the exported XML in using DataTable.ReadXml(fileName)? If you exported your table data using DataTable.WriteXml(file, XmlWriteMode.XmlSchema), then you can import it in to a new DataTable using ReadXml(file).
From an example in MSDN:
myOldTable.WriteXml(fileName, XmlWriteMode.WriteSchema);
DataTable newTable = new DataTable();
newTable.ReadXml(fileName);
If that's the case, I don't see where you'd need XPath. If I'm misunderstanding your question, please let me know and I'll update accordingly.
I hope this helps.
Related
We have created a custom dataset and populating it with some data.
Before adding data, we are adding columns in the data set as follows
DataSet archiveDataset = new DataSet("Archive");
DataTable dsTable = archiveDataset.Tables.Add("Data");
dsTable.Columns.Add("Id", typeof(int));
dsTable.Columns.Add("Name", typeof(string));
dsTable.Columns.Add("LastOperationBy", typeof(int));
dsTable.Columns.Add("Time", typeof(DateTime))
Once the Dataset is create, we are filling values as follows
DataRow dataRow = dsTable.NewRow();
dataRow["Id"] = source.Id;
dataRow["Name"] = source.Name;
dataRow["LastOperationBy"] = source.LastOperationBy;
dataRow["Time"] = source.LaunchTime;
Is there any better and managed way of doing this. can I make the code more easy to write using enum or anything else to reduce the efforts?
You could try using a Typed Dataset.
This should get rid of the ["<column_name>"] ugliness.
If the dataset has a structure similar to tables in a database, then Visual Studio makes it really easy to create one: just click Add -> New Item somewhere in the solution and choose DataSet. VS will show a designer where you can drag tables from your server explorer.
Update (after response to Simon's comment):
A typed dataset is in fact an XSD (XML Schema Definition).
What I did in a similar case was:
created an empty DataSet (using Add -> New Item -> DataSet)
opened the newly created file with a text editor (by dafault, in VS it shows the XSD designer)
paste the XSD that I had created manually
You could also choose to use the designer to create the schema.
Considering your comment "I am using Dataset to export data to a XML file" I recommend using a different technology such as
Linq to XML http://msdn.microsoft.com/en-us/library/bb387061.aspx or
Xml Serialzation http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializer.aspx
Or better yet of is doesnt have to be XML (and you only want hierarchical readable text consider JSON instead http://james.newtonking.com/pages/json-net.aspx
You can bind dataset in two way first one is using database second one is add manually.
After create column for dataset you can add using Loops you can add it if you have 10000 of entries.
You can use Reflection. Another option is to use EntityFramework or NHibernate to map the columnnames and datastructure columns and then avoid these code to fill each field manually. But they will add more complexity. Also Performance wise the your code is better.
I've got in-memory dataset with couple of tables that is populated in code. Data-bound grids on the gui show table contents without a problem.
Then I try to export the dataset into XML:
ds.WriteXml(fdSave.FileName, XmlWriteMode.WriteSchema);
and get empty XML (with couple of lines regarding dataset names but without any tables)
If I export table directly I've got all the data but dataset name is obviously wrong:
ds.Fields.WriteXml(fdSave.FileName, XmlWriteMode.WriteSchema);
What am I missing? Is there any reasonable way to write the whole dataset into file?
ok, silly me. was clearing dataset tables collection before populating it...
Have a look at the documentation on DataSet.WriteXml:
http://msdn.microsoft.com/en-us/library/ms135425.aspx
and on XmlWriteMode:
http://msdn.microsoft.com/en-us/library/system.data.xmlwritemode.aspx
Try using XmlWriteMode.IgnoreSchema. The docs say for XmlWriteMode.WriteSchema "If the DataSet does not have a current schema, nothing is written."
you must add the data tables to your dataset like,
ds.Tables.Add(datatable1);
then write export it into the schema
I would like to output a table to a webpage. The table is stored in an excel sheet (xls).
Is it possible to use xslt for this? The table is the cells are in this range:
A26 - P36 (16 columns and 11 rows)
If an exmaple file is need here is a link:
http://finans.opengate.dk/media/6704/2010-01-13.xls
Update: A daily file is uploaded. And I would like to automatically show a table from the latest xls-file using xslt. If some C# is needed to convert it from excel to something else (XML?) that is fine. It is done in the CMS Umbraco and that is why I hope to use XSLT since that is the way to show things in Umbraco, through xslt makroes.
BR. Anders
UPDATE with answer (based on answers below): No, it is not possible to read xls-files using xslt. If needed then one has to save excel sheet in another format xml or html. Or one will need a real programming language to read the excel file.
XSLT is mostly used to convert XML from one dialect to another, not to convert xls files to html.
If you just want to do this manually, you can save your worksheet as HTML directly in excel.
It is not clear from your question if you want to do this programmatically, and if so using what programming language.
You can use ADO.net to access cells in an excel file, similar to a DB query. This is a bit lighter than trying to use Excel automation objects.
http://support.microsoft.com/kb/316934
SpreadsheetGear for .NET can read Excel files and display them in a DataGrid as shown in the Excel to DataGrid sample on this page:
// Create a workbook from an Excel file
String ssFile = Server.MapPath("files/spiceorder.xls");
SpreadsheetGear.IWorkbook workbook = SpreadsheetGear.Factory.GetWorkbook(ssFile);
// Get a DataSet from an existing defined name
DataSet dataSet = workbook.GetDataSet("orderrange", SpreadsheetGear.Data.GetDataFlags.FormattedText);
// Bind a DataGrid to the DataSet
DataGrid1.DataSource = dataSet;
DataGrid1.DataBind();
SpreadsheetGear can also render png/gif/jpg images from cell ranges or charts as demonstrated here.
You can download the free trial here if you want to try it yourself.
Disclaimer: I own SpreadsheetGear LLC
I have a DataSet which has one table loaded with data.
When i write out the dataset to xml using the GetXml method of the dataset, i get all the columns in that data table as elements in the xml file.
How do i get the resulting xml with the column values as attributes instead of elements?
The article here trails off without a proper answer
I am using .NET Framework 2.0
Before writing the XML, do something like
foreach (DataColumn column in aDataSet.Tables[0].Columns)
{
column.ColumnMapping = MappingType.Attribute;
}
Although I'll admit I didn't test this. You still may get a DiffGram structured file.
foreach(DataColumn dc in dsHR.Tables[0].Columns)
dc.ColumnMapping = MappingType.Attribute;
Quite simple :)
I need a CSVParser class file
A Class File which parses csv and returns a dataSet as a result ASP.Net
I'm pretty sure that CSVReader (CodeProject) can read to DataTable.
DataTable table = new DataTable();
// set up schema... (Columns.Add)
using(TextReader text = File.OpenText(path))
using(CsvReader csv = new CsvReader(text, hasHeaders)) {
table.Load(csv);
}
Note that manually setting up the schema is optional; if you don't, I believe it assumes that everything is string.
Simple google gives plenty of results.
I've had luck with this parser. It will return results to a DataSet.
Another tool you might want to check out is FileHelpers. I see there's a tag for this resource here on SO.