xml instead db

  • Thread starter Thread starter mp
  • Start date Start date
M

mp

Hi,

How we can use xml file instead data base table (read/write).

Example:
CustID | FirstName | lastName | etc.

I am looking for some example.

Thanks
 
Hi,

What are you intending to do? Use ADO.NET with out a db backend?

If so, you will have to build the tables,relationships and dataset using
code, after that you can use the same way to query the dataset
after than yuo can serialize the dataset to a XML file and later load it
back.

Here is some code that can help you with it.

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

********************Loading the data ********************

if ( File.Exists( reportfile) )
{
LoadReportData();
}
else
{
CreateReportDS();
}

void LoadReportData()
{
reports = new DataSet();
StreamReader stream = new StreamReader(reportfile);
XmlTextReader reader = new XmlTextReader(stream);
reports.ReadXml(reader, XmlReadMode.ReadSchema);
reader.Close();
stream.Close();
}

************ Creating the data ********************

static void CreateReportDS()
{
reports = new DataSet("Reports");

#region The Report table
DataTable table = new DataTable("Report");
DataColumn pkCol = table.Columns.Add("ID", typeof(int));
pkCol.AutoIncrementSeed=0;
pkCol.AutoIncrement = true;
table.Columns.Add("Location", typeof(string));
table.Columns.Add("Exported", typeof(bool));
table.PrimaryKey = new DataColumn[] {pkCol};
reports.Tables.Add( table);
#endregion

#region The Answer Table
table = new DataTable("Answer");
pkCol = table.Columns.Add("ID", typeof(int));
pkCol.AutoIncrement = true;
pkCol.AutoIncrementSeed = 1;
table.Columns.Add("answer", typeof(char));
table.Columns.Add("comments", typeof(string));
table.PrimaryKey = new DataColumn[] {pkCol};
reports.Tables.Add( table);
#endregion

#region The ReportAnswer Report Relationship
DataRelation fkRel = new DataRelation( "Report_Answer",
reports.Tables["Report"].Columns["ID"],
reports.Tables["Answer"].Columns["rID"]);
reports.Relations.Add( fkRel);
#endregion

}
******************** Saving the data ********************

StreamWriter stream = new StreamWriter( reportfile);
XmlTextWriter writer = new XmlTextWriter(stream);
reports.WriteXml( writer, XmlWriteMode.WriteSchema);
writer.Close();
stream.Close();
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top