Solved

Append datarow to the end of an xml file

Posted on 2004-10-19
8
912 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
 

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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 125 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

The Problem How to write an Xquery that works like a SQL outer join, providing placeholders for absent data on the outer side?  I give a bit more background at the end. The situation expressed as relational data Let’s work through this.  I’ve …
Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

705 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now