PDA

View Full Version : Write to xml - ADO.Net



Ed Jobe
2004-08-30, 08:43 PM
I have a ds/schema that reads from an xml. Help mentions that you can update the rows that have changed by getting the ds's changes, bringing them into a temp ds and using the same DataAdapter to update the db with the changes. However, the DA uses a Connection object and I don't see one for working with an xml file. The ds can directly write to the file, but it writes the whole ds. It doesn't have an Update method like the DA does and the AcceptChanges method doesn't update the file. Any suggestions?

mjfarrell
2004-08-30, 08:53 PM
Any help at XML.org's website?

Ed Jobe
2004-08-30, 09:18 PM
I havn't checked there. I'll add that to my resources. I've been doing some more research and I think that an XmlDataDocument might be what I need. Going to try it out when I get home.

mtuersley
2004-09-06, 05:28 AM
Ed, you may want to look into using XPath which is simply using SQL statements to hit your XML file *if* I read your post accurately.

sinc
2004-09-06, 03:30 PM
I haven't used .NET yet, so what follows is general programming concepts. You'll have to figure out how it applies.

Assuming .NET follows good programming principles (which I've been told it does, mostly, especially considering it's a Microsoft product), then a data source holds peristent data (data that is still available the next time you run the program) in a form that the program can use. In order to get data between this data source and your data store (the database, flat-file, or whatever else you may desire), you need an adapter that knows how to talk to that type of data store. You therefore need TWO adapters - the one for your database, and the one for an XML file.

From what you say of the documentation, it seems to indicate that .NET does not provide an adapter for XML files. Therefore, what it's telling you to do is parse the XML file in your program, save the data to a new datastore that knows how to talk to your database, and then tell that datastore to update the database. The new data will now be in your database, and accessible from your main datastore.

In short, they seem to be saying "since we didn't write an adapter for you, follow this hack procedure to accomplish the task." Alternatively, you might be able to find a third-party XML adapter out there somewhere - as something that should have been included in the core product, a bunch of people probably have them available by now.

Ed Jobe
2004-09-07, 04:29 PM
What's hard for me about moving to .Net is learning the extensive object model. I know what I want to do, but what object does that? There is no data adapter for xml, as that is the native format intended for data. There are plenty of overrides for handling xml, I just needed to find a way that kept the data structure intact. XmlDataDocument was the correct object. With it, you can even develop a schema for only part of an xml database and update just that part, without affecting the rest of the file. Or update the whole file as a single dataset. My only problem now is finding out how ADT manages to allow other ADT users to access the file, because the project browser keeps the file open and my app can't save any changes while its open. It can only read the file. A cool thing about xml streams, you can easily read a file without getting error messages when a file is already open.

mtuersley
2004-09-08, 03:19 AM
You actually do not need any data adapters to connect to a data source in .NET - same as you do not need a data control in VB6. Data adapters have as many pros as they have cons. When dealing with XML data, the ability to interact with it is internal to the .NET framework. There is nothing lacking in .NET's interaction with XML files - it does more easier, faster and with less code than ADO2.8 in the COM world.

Ed's issue is with Autodesk's locking of the ADT project file. First point here is that it is not a *true* XML file in that it does not meet the core concept of being 'well formed'. Autodesk is taking XML through their own spin which is an issue when dealing with it. The reasoning may very well be to allow for the lock/unlock editing scenario. Second point is true XML files are meant to be a transferable medium not stationary, so many of the standard web/msdn info does not apply. Last problem is in the method of attack by the programmer. ADO.NET has nothing to do with ADO2.8 other than in name. The .NET version is a new beast that is extremely power and cool once you lose the COM-based trappings you'll take into it - trust me =) I learned that lesson only too well! So can be your own worst enemy.

Ed - let me do some checking with a few of my Autodesk contacts and see if I can come up with something for you. No promises other than I'll raise the issue!

Ed Jobe
2004-09-08, 02:53 PM
Thanks Mike. For now, I've given up on that. I'm just going to put it out so at least they can edit the apj when no one is working on it. Its better than using plain xml editors.

Ed Jobe
2004-09-14, 09:11 PM
New problem: I'm using an xmlDataDocument and a dataset. I bring a schema into the dataset and create a datadoc with the dataset, then datadoc.Load("apjpath") to get the data in. I want to let the user successively open files, but after the first load, I get errors like "can't load a doc while one is already loaded" or "can't change the dataset on an open doc", depending on how I try to work around this. There doesn't seem to be a way to dispose of a datadoc. I tried setting it to nothing, using the Clear method on the dataset (the data doc doesn't support the Clear method). What's the proper way to successively load xmldatadocs?

Ed Jobe
2004-09-24, 07:44 PM
Here is the code I have so far, besides the automatically generated code.


Private Sub Button1_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Button1.Click
'This button gets an xml file to load and populates the dataset/xmlDataDoc,
'which is bound to a datagrid. It runs fine the first time.
'If the user want to load a different file,
'the second time it is used the code errors as noted below.
If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
CurrentFile = OpenFileDialog1.FileName
'enable save button
Me.Button3.Enabled = True

'Load schema into the dataset.
'This will error on 2nd use: Can't change schema when attached
'to a loaded xmlDataDocument.
NewDataSet1.ReadXmlSchema(SchemaFileName)

'load the schema into a new xmlDataDocument
xDataDoc = New Xml.XmlDataDocument(NewDataSet1)

'validate data against schema)
xDataDoc.Load(CurrentFile)
NewDataSet1.AcceptChanges()
Me.Text = Me.Text & " - " &
NewDataSet1.Project.Rows.Item(0).Item("Name")
Try
'test for read-only
xDataDoc.Save(CurrentFile)
Catch
MsgBox(CurrentFile & " is in use or is read-only.", _
MsgBoxStyle.Exclamation, "Read-only file")
Me.Text = Me.Text & " - ReadOnly"
Me.Button3.Enabled = False
End Try

End If

End Sub
Since I can't unload the xmlDataDoc, I've tried various schemes to use a new
one or reset the dataset or both but always run into some error, e.g. if I
try to reset the dataset, I get an error on the datadoc and vice versa.

mtuersley
2004-09-27, 03:19 AM
Hi Ed,

I will look over this code some time tomorrow. In the meantime, here is a quickie sample app that might answer your question or get you moving in the right direction. Included in the zip are two DAT files which are serialized XML. It is written in VS2003 - if you have 2002, you'll need to start a new project and just pull all my files in.