Click here to Skip to main content
12,688,944 members (31,183 online)
Rate this:
Please Sign up or sign in to vote.
See more: C# ADO.NET
hey guys, quick question thats been bugging me for a few hours.

i can retrieve an xml file from a directory that i specify really easy, i store this in a dataset to be passed to a datagrid etc..

now what im trying to do is save this exact file as it stands to a different directory, and i just cant figure it out, im trying to pass the current dataset to this object so that when i click the save button, it will save this xml file, say, on my desktop.

this is the code im using to save the file on desktop:

private void XMLpathBTN_Click(object sender, EventArgs e)
            string myXMLFile = "F:\\year2\\Data Cent Win Prog\\Assignemnt 2\\Assignment2\\Assignment2\\Books.xml";

            SaveFileDialog saveFile = new SaveFileDialog();

            saveFile.DefaultExt = "xml";
            saveFile.AddExtension = true;
            saveFile.InitialDirectory = @"C:\Users\Matthew\Desktop\";
            saveFile.OverwritePrompt = true;
            saveFile.Title = "Save as XML File";
            saveFile.FileName = "copiedXML";
            saveFile.ValidateNames = true;

            if (saveFile.ShowDialog() == DialogResult.OK)

                ds.WriteXml(saveFile.FileName, XmlWriteMode.WriteSchema);

i know why its writing a blank dataset is because there is nothing in it, i want to pass the data from the load click event to this event so it has the same data ready from copying, ill post the load event aswell:

private void importXML_Click(object sender, EventArgs e)

            OpenFileDialog loadXMLFile = new OpenFileDialog();

            BindingSource bs = new BindingSource();

            loadXMLFile.DefaultExt = "xml";
            loadXMLFile.AddExtension = true;
            loadXMLFile.InitialDirectory = @"F:\year2\Data Cent Win Prog\Assignemnt 2\Assignment2\Assignment2\Books.xml";
            loadXMLFile.Title = "Loan an XML File";
            loadXMLFile.FileName = "Books";//importXMLTB.Text = @"F:\year2\Data Cent Win Prog\Assignemnt 2\Assignment2\Assignment2\Books.xml";
            loadXMLFile.ValidateNames = true;

            if (loadXMLFile.ShowDialog() == DialogResult.OK)
                importedDS.ReadXml(loadXMLFile.FileName, XmlReadMode.InferTypedSchema);

                bs.DataSource = importedDS.Tables[0];

                DGTables.DataSource = bs;


just want to pass the above file to the save button file but i cant figure it out
Please help
Posted 8-May-12 6:15am
Clifford Nelson 8-May-12 12:06pm
Are you getting an error when you save the file? Have you stepped through the code so you know it is executing?
BBCokeley 8-May-12 12:22pm
there was no error it was writing an empty xml file, because the dataset i was passing it was also empty.

ive solved this below.
thank you for your comment.

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Strange solution, but here we go.

what i did was pretty much use the code from my load into the data grid function, this was it loaded into a dataset ready to be written to the xml file at a location of my choice.

if you would like to see how it is done please post :)


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.170113.4 | Last Updated 8 May 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100