PC Review


Reply
Thread Tools Rate Thread

Add DataRow to DataTable and Save as XML

 
 
=?Utf-8?B?RmFicmljaW8=?=
Guest
Posts: n/a
 
      12th Feb 2005
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

 
Reply With Quote
 
 
 
 
=?Utf-8?B?RmFicmljaW8=?=
Guest
Posts: n/a
 
      13th Feb 2005
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
>

 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Delete DataRow In DataTable Marina Microsoft VB .NET 0 24th Aug 2005 05:00 PM
Re: Delete DataRow In DataTable Cor Ligthert [MVP] Microsoft VB .NET 0 24th Aug 2005 04:59 PM
Add DataRow to DataTable and Save as XML =?Utf-8?B?RmFicmljaW8=?= Microsoft VB .NET 1 13th Feb 2005 02:01 AM
DataRow and DataTable C# beginner Microsoft C# .NET 1 23rd Feb 2004 03:57 PM
Getting a DataRow From A DataTable Eyal Cohen Microsoft C# .NET 2 24th Oct 2003 08:59 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:33 PM.