473,395 Members | 1,383 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

C# App - How can you update source XML file from dataset/dataview?

AHayes
10
I've been searching online for a while now with no luck, so I'll try the friendly folk here.


_The Problem
I have a dataset/dataview that's getting its data from an XML file (which we're using sort-of like a database). I'm a little more used to contacting SQL servers instead of XML files, but have managed to read from the XML file and read/display the data. I thought I was able to change the data (from a form) as well, but realized the changes to the data were only in the "memory" version of the table and NOT the source document like I want.

This is a problem I ran into when I started using ADO.Net with SQL server, so I know the issue is simply filling in or updating the source file with the changes...I just don't know how to do that in the XML world. So I guess I'm looking for the XML equivalent to "SQLDataAdapter.Fill(DataSet)".


_The Code
Here's a sample of the code as I have it currently:

Expand|Select|Wrap|Line Numbers
  1. //Initialize dataset/dataview
  2. DataSet dstMyDataset = new DataSet();
  3. DataView dvwMyDataview = new DataView();
  4.  
  5. //Import XML
  6. String strFilePath = Application.StartupPath;
  7. dstMyDataset.ReadXmlSchema(strFilePath + "\\DataDef.xsd");
  8. dstMyDataset.ReadXml(strFilePath + "\\Data.xml");
  9.  
  10. //Example edit of data from form:
  11. dvwMyDataview[0]["Phone"] = txtPhone.Text;
  12.  
  13. //Saving
  14. dstMyDataview.WriteXml(strFilePath + "\\Data.xml);





As always, I appreciate the help.
Oct 22 '07 #1
7 4965
r035198x
13,262 8TB
I've been searching online for a while now with no luck, so I'll try the friendly folk here.


_The Problem
I have a dataset/dataview that's getting its data from an XML file (which we're using sort-of like a database). I'm a little more used to contacting SQL servers instead of XML files, but have managed to read from the XML file and read/display the data. I thought I was able to change the data (from a form) as well, but realized the changes to the data were only in the "memory" version of the table and NOT the source document like I want.

This is a problem I ran into when I started using ADO.Net with SQL server, so I know the issue is simply filling in or updating the source file with the changes...I just don't know how to do that in the XML world. So I guess I'm looking for the XML equivalent to "SQLDataAdapter.Fill(DataSet)".


_The Code
Here's a sample of the code as I have it currently:

Expand|Select|Wrap|Line Numbers
  1. //Initialize dataset/dataview
  2. DataSet dstMyDataset = new DataSet();
  3. DataView dvwMyDataview = new DataView();
  4.  
  5. //Import XML
  6. String strFilePath = Application.StartupPath;
  7. dstMyDataset.ReadXmlSchema(strFilePath + "\\DataDef.xsd");
  8. dstMyDataset.ReadXml(strFilePath + "\\Data.xml");
  9.  
  10. //Example edit of data from form:
  11. dvwMyDataview[0]["Phone"] = txtPhone.Text;
  12.  
  13. //Saving
  14. dstMyDataview.WriteXml(strFilePath + "\\Data.xml);







As always, I appreciate the help.
I think you need the a filestream object as well as explained here.
Are you making changes to data/schema or both?
Oct 22 '07 #2
AHayes
10
I think you need the a filestream object as well as explained here.
Are you making changes to data/schema or both?

The changes being made are just to the data.



I tried the filestream approach, but it doesn't fix the issue. If the dataset itself isn't updated with the changes being made, how would changing how it gets written matter?


If I make a change to data within a DataView, how do I update the DataSet with said changes?
Oct 22 '07 #3
r035198x
13,262 8TB
The changes being made are just to the data.



I tried the filestream approach, but it doesn't fix the issue. If the dataset itself isn't updated with the changes being made, how would changing how it gets written matter?


If I make a change to data within a DataView, how do I update the DataSet with said changes?
AcceptChanges ?
Oct 22 '07 #4
AHayes
10
AcceptChanges ?

No. That's not doing anything either.


Just to add to the background:


I can launch my application, go to the form that the user will edit the data from (it fills in textboxes with a single record from the XML file so the user can change something) and edit something and "save" it.

I close my form and open it, and my change to the data is displayed correctly still. However, once I close the entire app and relaunch, it reverts back to the XML file--which was not actually changed.


Like I said, I had this issue once before, and I had to use SQLDataAdapter's Fill() method to have it update the dataset. Since I'm not using SQL server with my current app, I can't do this. I'm not sure if there's an XML equivalent.




Now, I've also tried displaying a test result FROM THE DATASET as such:
MessageBox.Show(dstConfig.Tables["TABLE_NAME"].Rows[0]["FEILD_NAME"].ToString());

..and that will display the data change correctly.



It feels like I'm missing a step....and I guess not a whole lot of people use XML like a database, since I can't seem to find a tutorial anywhere online that isn't connecting to SQL Server DB or Access or something.
Oct 22 '07 #5
nateraaaa
663 Expert 512MB
If you are using 2.0 there is a statement that may help you.
DataView.ToTable().WriteXml(...)

If not I believe you can only use the WriteXml method on a dataset. In your code you are trying to do the WriteXml method on a dataview.

See this discussion .

Nathan
Oct 22 '07 #6
AHayes
10
If you are using 2.0 there is a statement that may help you.
DataView.ToTable().WriteXml(...)

If not I believe you can only use the WriteXml method on a dataset. In your code you are trying to do the WriteXml method on a dataview.

See this discussion .

Nathan


Sorry. I didn't notice when I was changing the variable names in the sample code. I AM--in the REAL code--using DataSet.WriteXML(). It just doesn't seem to be writing.


I'll try the alternative method and get back to everyone. Thanks.
Oct 23 '07 #7
AHayes
10
Just FYI to close this thread out. I've found the issue was a lot simpler then I was making out to be (my apologies):

We were renaming our XML files to a different extension (to make it a little less obvious that it's just a giant XML file). Although it was still reading the XML file correctly into a dataset, I guess WriteXML() isn't as nice. Thus, it was never re-writing the file/saving changes.


So note to any kids out there: Don't rename your file extensions.
Oct 23 '07 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Bill Salling | last post by:
am trying to updated the source data(Access2000) for my data grid. But keep getting this error An unhandled exception of type 'System.InvalidOperationException' occurred in system.data.dl ...
7
by: skyjoker1 | last post by:
I think what I'm trying to do is something relatively simple. I have created a DataTable using the SQLDataAdapter.Fill method: Dim da As New SqlDataAdapter("SELECT * FROM Trans", cn) Dim ds As...
3
by: DraguVaso | last post by:
Hi, I have to update Some records in a DataSet, I now use this functions: Dim NewDataRow As DataRow 'dtsXmldFormSettings = the DataSet NewDataRow =...
9
by: jaYPee | last post by:
I have search a lot of thread in google newsgroup and read a lot of articles but still i don't know how to update the dataset that has 3 tables. my 3 tables looks like the 3 tables from...
15
by: graham | last post by:
Hi all, <bitching and moaning section> I am asking for any help I can get here... I am at the end of my tether... I don;t consider myself a genius in any way whatsoever, but I do believe I have...
9
by: Michael | last post by:
Hi, I have a large table. Normally, the user will need to see only the data from the past two years, but sometimes, they will need to go back further. I have a form with a datagrid, a...
0
by: nospamthanks | last post by:
Hi, This is probably a very simple question, but it's held me up for the last hour or two: I have a simple xml document and schema - all I am doing for the moment is loading the xml into a...
2
by: William LaMartin | last post by:
I have report based on a datasource (a tableadapter) that supplies names, street addresses, cities, etc. to be displayed in a report viewer. Is there a way in code to modify this report to only...
2
by: rrflore2 | last post by:
Ok. I'm writing and deleting to an xml file using a dataset. I have a function in my codebehind page that binds a listbox to the dataset that performs the writes/deletes. Everything seems to be...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.