Solved

Append datarow to the end of an xml file

Posted on 2004-10-19
8
913 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
XML namaspace 2 44
Viewing XML as a table on a Mac 3 89
Strange html entity encoding by tidy 14 79
XML to Excel using XSL - formatting 3 26
The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

895 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

13 Experts available now in Live!

Get 1:1 Help Now