Solved

Append datarow to the end of an xml file

Posted on 2004-10-19
8
921 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Containers and Docker for Everyone

Containers are an incredibly powerful technology that can provide you and/or your engineering team with huge productivity gains. Using containers, you can deploy, back up, replicate, and move apps and their dependencies quickly and easily.

 

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

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

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

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…
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…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

726 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