?
Solved

Append datarow to the end of an xml file

Posted on 2004-10-19
8
Medium Priority
?
928 Views
Last Modified: 2012-08-14
I am writing in VB.NET and working with an ADO.NET dataset.

In most cases I can use the dataSet.WriteXML method to backup my dataset as an xml file.  But for the most part I don't want to have to rewrite the xml file everytime I add a new row to the dataset.  How would I go about appending just the new row data to the end of the xml file or do I just have to rewrite it everytime?
0
Comment
Question by:misslinda454
  • 3
  • 3
  • 2
8 Comments
 
LVL 26

Expert Comment

by:rdcpro
ID: 12352834
You can use the GetChanges method:

changedDataSet = myDataSet.GetChanges(DataRowState.Modified)

then write the changedDataSet to an XML document.  If there's only a single row in it, you can then append the appropriate node to the previous XML document.  Otherwise you'd have to iterate over the changed nodes, appending each one.  Not sure if that saves you anything, though, unless the data is very large and you keep the original XML in memory.

Regards,
Mike Sharp
0
 
LVL 15

Expert Comment

by:dualsoul
ID: 12355440
J_Make sorry, i've missed it, it's all right with your Schema
0
 
LVL 15

Expert Comment

by:dualsoul
ID: 12355442
upppssss....wrong topic, sorry guys ;)
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:misslinda454
ID: 12357602
Would you happen to have an example of appending a datarow to the data set?  Does this recognize the schema for you or do you have to take each field and know the schema?  If you use the get changes method are you still only writing the changed rows or are you still re-writing the whole xml doc .. if it only goes through and writes the changes that would be perfect!
0
 
LVL 26

Expert Comment

by:rdcpro
ID: 12361154
Well, MSDN has this example:

Private Sub UpdateDataSet(ByVal myDataSet As DataSet)
   ' Check for changes with the HasChanges method first.
   If Not myDataSet.HasChanges(DataRowState.Modified) Then Exit Sub
   ' Create temporary DataSet variable.
   Dim xDataSet As DataSet
   ' GetChanges for modified rows only.
   xDataSet = myDataSet.GetChanges(DataRowState.Modified)
   ' Check the DataSet for errors.
   If xDataSet.HasErrors Then
      ' Insert code to resolve errors.
   End If
   ' After fixing errors, update the data source with the DataAdapter
   ' used to create the DataSet.
   myOleDbDataAdapter.Update(xDataSet)
End Sub


Regards,
Mike Sharp
0
 

Author Comment

by:misslinda454
ID: 12361294
I knew how to update a DB using a data adapter and dataset ... I didn't know how to add a row to the xml document  .. I know how to append a child node at a time just not a row - a collection of nodes for lack of better words ..

So if I had a row that had data in Field1, Field2, Field3 .. and I wanted to add that row to the bottom of the data set with a doc that looked like ..

<datatable1>
   <row>
     <field1> a </field1>
     <field2> b </field2>
     <field3> c </field3>
   </row>
   .....how to append next  row to end?????
</datatable>

0
 
LVL 26

Accepted Solution

by:
rdcpro earned 500 total points
ID: 12362142
Oh, I see.  In order to append a row, just append the parent node.  In your example, appending a new <row> will attach it, along with it's descendants.  But before that point, when you copy the node (or import it) you set a boolean value to define whether you want a deep copy or not.  Deep, meaning the node and it's descendants.  But the operation of appending is otherwise the same.

Here's an example of Importing nodes from one XML to another.  It's written in C#, but the VB approach is similar, except for syntactical differences.  In .NET, you can't simply append nodes from one document to another.  But you can create nodes using methods of the XmlDocument class, and append them to the same instance of the class.  

http://rdcpro.com/Members/rdcpro/snippets/importnode/

A javascript example, using MSXML, that clones or moves nodes is found here:

http://rdcpro.com/Members/rdcpro/snippets/moveorclone/

Regards,
Mike Sharp
0
 

Author Comment

by:misslinda454
ID: 12362178
Perfect! Thank you very much!
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
Create a Windows 10 custom Image with custom task bar and custom start menu using XML for deployment.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses
Course of the Month15 days, 14 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question