PROBLEM WITH OpenFileDialog WITH XML

B

bobrad

MAYBE SOMEBODY CAM HELP

I AM USING THE FOLLOWING CODE TO GET A LOCATION OF A FILE IN A WINFORM
DIALOG

OpenFileDialog openFileDialog = new OpenFileDialog();
OpenFileDialog.Filter = " Access (*.mdb)|*.mdb|All files (*.*)|*.*";
if (openFileDialog.ShowDialog() == DialogResult.OK)
DataBaseLocation.Text = openFileDialog.FileName;

THIS WORKS GREAT AND I GET THE LOCATION PUT IN A TEXT BOX
HERE COMES THE PROBLEM, I WANT TO SAVE THIS LOCATION IN AN XML FILE
(DOCUMENT) STORED ON THE PC. HERE GOES MY CODE FOR XML


XmlTextWriter TW=new XmlTextWriter("dbfile.xml",null);
TW.WriteStartDocument();
TW.WriteStartElement("Database");
TW.WriteElementString("DatabaseLoc",cf.DataBaseLocation.Text);
TW.WriteEndElement();
TW.WriteEndDocument();
TW.Close();

THIS CODE WORKS FINE ONLY IF I TYPE IN THE LOCATION INTO THE TEXTBOX
MSELF, I AM THAN ABLE TO CREATE AN XML FILE ON THE PC. IF I USE THE
OPENFILEDIALOG TO SELECT A FILE, THIS IS WHERE I HAVE THE PROBLEM.
FIRST IT DOES NOT CREATE A XMLFILE AT ALL EVEN THOGUH THE INFORMATION
SHOWS UP IN THE TEXT BOX, IF I CLOSE THIS DIALOG AND REOPEN IT GIVES
THE IMPRESSION IT READ THE XML FILE AND DISPLAY THE FILE I SELECTED
EVEN THOUGH THERE IS NO PHYSICAL XML FILE, IT APPEARS AS IF IT'S
STILL IN MEMORY AND NOT COMMITING THE WRITE. IF I EXIT THE
APPLICATION AND RESTART IT I CAN NO LONGER PULL UP THE XML FILE.

doES ANYONE HAVE ANY IDEA WHAT THE PROBOLE CODE BE, AM I USING A
REFERNCE RATHER THAN ACTUAL STRING, THATS WHY XML CAN'T COMMIT TO THE
WRITE? OR IS THIS A BUG? ANY HELP ON THIS WOULD BE GREAT

THANKS
 
N

NaraendiraKumar R. R.

I was able to make it work with a minor change. I dropped cf in the line
below.
TW.WriteElementString("DatabaseLoc",cf.DataBaseLocation.Text);

If that does'nt work & you still think that the content in memory is not
being commited to a disk, try doing Flush() on the Writer.

-Naraen

-----
 
B

bobrad

thanks - but It's still a problem

the cf was a typo on my part in posting here,
i have posted the entire dialog that I used below

I am also using VS 2003




using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Xml;

namespace test
{
public class DataBaseSetup : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label5;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.TextBox databaseloc;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.OpenFileDialog openFileDialog1;

private System.ComponentModel.Container components = null;
public DataBaseSetup()
{
InitializeComponent();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

private void InitializeComponent()
{
this.label5 = new System.Windows.Forms.Label();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.databaseloc = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
this.groupBox1.SuspendLayout();
this.SuspendLayout();
//
// label5
//
this.label5.Location = new System.Drawing.Point(72, 40);
this.label5.Name = "label5";
this.label5.TabIndex = 8;
this.label5.Text = "Database Setup";
//
// groupBox1
//
this.groupBox1.Controls.Add(this.button1);
this.groupBox1.Controls.Add(this.databaseloc);
this.groupBox1.Location = new System.Drawing.Point(64, 72);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(464, 208);
this.groupBox1.TabIndex = 9;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "groupBox1";
//
// databaseloc
//
this.databaseloc.Location = new System.Drawing.Point(120, 80);
this.databaseloc.Name = "databaseloc";
this.databaseloc.Size = new System.Drawing.Size(320, 20);
this.databaseloc.TabIndex = 0;
this.databaseloc.Text = " ";
//
// button1
//
this.button1.Location = new System.Drawing.Point(24, 80);
this.button1.Name = "button1";
this.button1.TabIndex = 1;
this.button1.Text = "database";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(392, 304);
this.button2.Name = "button2";
this.button2.TabIndex = 10;
this.button2.Text = "button2";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// DataBaseSetup
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(560, 349);
this.Controls.Add(this.button2);
this.Controls.Add(this.groupBox1);
this.Controls.Add(this.label5);
this.Name = "DataBaseSetup";
this.Text = "DataBaseSetup";
this.groupBox1.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
private void button1_Click(object sender, System.EventArgs e)
{
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Filter = "DentalXpress Access (*.mdb)|*.mdb|All files
(*.*)|*.*";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
databaseloc.Text = openFileDialog1.FileName.ToString();
}//end if
}

public void createConfigFile()
{
try
{
MessageBox.Show("db = " + databaseloc.Text);
XmlTextWriter tw=new XmlTextWriter("dbconfigfile.xml",null);
tw.WriteStartDocument();
tw.WriteStartElement("DatabaseConnection");
tw.WriteElementString("DatabaseLoc",databaseloc.Text);
tw.WriteEndElement();
tw.WriteEndDocument();
tw.Flush();
twwriter.Close();
}
catch
{
MessageBox.Show("xmlcreaterror");
}

}
private void button2_Click(object sender, System.EventArgs e)
{
createConfigFile();
this.Close();
}//end createConfigFile()
}
 

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

Top