473,387 Members | 1,742 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Add DataRow to DataTable and Save as XML

My XML file has the following information:

-----------------------------------------------------------
<data>
<students>
<student>
<name>Rick James</name>
</student>
<student>
<name>Peter Gomez</name>
</student>
</students>
</data>
-----------------------------------------------------------

I use a DataSet to read the file and then attempt to add an extra <student>
as follows:

-----------------------------------------------------------
DataSet ds;
DataTable dt;
DataRow dr;

ds = new DataSet("data");
ds.ReadXml("data.xml");
dt = ds.Relations["students_student"].ChildTable;

dr = dt.NewRow();
dr["name"] = "Farty McSniff";
dt.Rows.Add(dr);

ds.AcceptChanges();
ds.WriteXml("data.xml");
-----------------------------------------------------------

The new <student> does NOT become a child of the <students> tag, instead it
gets added as a child of the <data> tag. Here's the output:

-----------------------------------------------------------
<data>
<students>
<student>
<name>Rick James</name>
</student>
<student>
<name>Peter Gomez</name>
</student>
</students>
<student>
<name>Farty McSniff</name>
<student>
</data>
-----------------------------------------------------------

My question: How do I make the new <student> a child of the <students> tag?

Thanks in advance,
-Fabricio

Nov 16 '05 #1
1 6133
I figured it out... I wasn't taking into account the relationship between the
parent and child node. The DataSet automatically creates a hidden ID column
to represent the structural relationship found in the XML file. Setting the
hidden ID of the NewRow accordingly solved the problem.

-Fabricio

"Fabricio" wrote:
My XML file has the following information:

-----------------------------------------------------------
<data>
<students>
<student>
<name>Rick James</name>
</student>
<student>
<name>Peter Gomez</name>
</student>
</students>
</data>
-----------------------------------------------------------

I use a DataSet to read the file and then attempt to add an extra <student>
as follows:

-----------------------------------------------------------
DataSet ds;
DataTable dt;
DataRow dr;

ds = new DataSet("data");
ds.ReadXml("data.xml");
dt = ds.Relations["students_student"].ChildTable;

dr = dt.NewRow();
dr["name"] = "Farty McSniff";
dt.Rows.Add(dr);

ds.AcceptChanges();
ds.WriteXml("data.xml");
-----------------------------------------------------------

The new <student> does NOT become a child of the <students> tag, instead it
gets added as a child of the <data> tag. Here's the output:

-----------------------------------------------------------
<data>
<students>
<student>
<name>Rick James</name>
</student>
<student>
<name>Peter Gomez</name>
</student>
</students>
<student>
<name>Farty McSniff</name>
<student>
</data>
-----------------------------------------------------------

My question: How do I make the new <student> a child of the <students> tag?

Thanks in advance,
-Fabricio

Nov 16 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Michael Carr | last post by:
I have a function that populates a class with values from a database. I'd like to pass into the function either a SqlDataReader or a DataRow, depending on which mechanism I'm using to retrieve data...
1
by: Arpan | last post by:
This is how I am dynamically adding a table to a DataSet: <script runat="server"> Sub Page_Load(ByVal obj As Object, ByVal ea As EventArgs) 'Create an empty DataSet Dim dSet As New DataSet ...
5
by: samoore33 | last post by:
I use the code below to search through a DataSet: Dim t As DataTable t = result.Tables("State") Dim strExpr As String strExpr = "id = '" & theState.ToString() & "'" Dim foundRows() As DataRow...
6
by: Pete Wittig | last post by:
Hi, I have a DataTable and I want to get a subset of the rows within it. I use the Select method to get my subset and the results are in a DataRow. I want to put those Rows back into a...
10
by: mcbobin | last post by:
Hi, Here's hoping someone can help... I'm using a stored procedure to return a single row of data ie a DataRow e.g. public static DataRow GetManualDailySplits(string prmLocationID, string
2
by: Martin Z | last post by:
I'm just learning ADO.NET 1.1 and I'm getting some real stinkers. I've got a wonderful screwup going on - I'm loading my datatable with a new datarow. I used BeginLoadData before I added the row...
5
by: Rainer Queck | last post by:
Hello NG, what would be the best way to locate a DataRrow in a DataGridView? I have by DataTable.Select a bunch of DataRows from a DataTable (which is the data source to the DataGridView). Now...
2
by: =?Utf-8?B?TWFyYw==?= | last post by:
In Visual Studio 2005, I am developing a Windows Mobile application, using Mobile SQL 2005. I need Data from a Database to be shown in a DataGrid, this works. But now I want to be able to get the...
6
by: cj | last post by:
If I'm sitting on a datarow for a customer and want to change his phone number only if it's blank what would I write? if myDr("phone")= "" then myDr("phone") = "mphone" endif...
1
by: SunshineInTheRain | last post by:
Dim dtUn As DataTable dtUn= New DataTable dtUn = BindICUN() Dim i As Integer Dim irow As DataRow Dim mailBody As String = "" Dim myRow As...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.