PDA

View Full Version : Using C# to store data as XML


MattyJim
04-09-2005, 02:20 PM
I have a C# assignment to complete, which requires me to ceate a userform and output the fields to XML.

The XML file is intended to act as a database, and all information should be updatable, deletable, etc, etc.

Everything works fine so far; I've managed to create the XML file and write a schema that can be updated with a new datacolumn every time the user clicks a particular button.

When I check the XML file, the datacolums have been added as required.

My main task now is to write datarows to the datacolumn, so that I could, for example, have a datacolumn that related to a particular person, and which held details in datarows about that particular person (name, address, etc).

I NEED THE XML FILE TO BE UPDATED WITH A NEW COLUMN AND ACCOMPANYING ROWS EVERY TIME THE USER CLICKS A BUTTON.

The fragment of code shown below adds datacolumns just fine, but when I check the XML file, the datarows are never there: -


(Everything here is held within a button_click event, by the way, and the datacolumn names come from textboxes on my form)

DataSet ds = new DataSet();


FileStream finschema = new FileStream(DirName + "//myDataFile.xml",
FileMode.Open, FileAccess.Read, FileShare.Read);

ds.ReadXmlSchema(finschema);

finschema.Close();



FileStream fout = new FileStream(DirName + "//myDataFile.xml",
FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite);

DataColumn newcolumn = new DataColumn("Column " + textBox1.Text);
ds.Tables[0].Columns.Add(newcolumn);

DataRow myRow;
myRow = ds.Tables[0].NewRow();
// Then add the new row to the collection.
myRow["Column " + textBox1.Text] = textBox2.Text;
ds.Tables[0].Rows.Add(myRow);

ds.WriteXmlSchema(fout);

fout.Close();


:confused: Any of you crazy computer geniuses out there got any ideas, coz I'm tearing my hair out at the minute!!! :confused:


Any help that you could offer would be greatly appreciated!!


Thanks,


Matt :)

oracleguy
04-09-2005, 09:34 PM
Well, if you want actual data to be written to the XML file, you need to use the WriteXML() method instead of the WriteXMLSchema() method because the latter will do just what it says, write the schema but not the data. That may fix your problem.

MattyJim
04-10-2005, 02:02 PM
Thanks for your reply!

Yeah, I've tried WriteXml, but my output's still the same.

One thing I've noticed is that I'm presently using an untyped dataset; would that make any difference do you think?

A guy from another forum recommended accessing my Xml file like: -

string path = Server.MapPath("MyDataFile.xml");
DataSet1.ReadXml(path); //DataSet1 being a TYPED dataset

The only problem is that I get the "The type or namespace name 'Server' could not be found..." error when I use this code, apparently regardless of the directives I include.

I've tried "using System.Web", as the Microsoft Help files indicated that "Server" was related to this somehow, but to no avail.

Any idea what I might be doing wrong?


Cheers,


Matt.