PDA

View Full Version : Problems when adding new rows in .xml (using C#)


MattyJim
07-12-2005, 01:54 AM
Hi!


I'm trying to add new rows to an .xml file using a 'typed dataset'.


The code used to add the entries is as follows:


string[] strArray = new string[9]
{
txtID.Text, txtName.Text //These are fields on a UserForm
};

DataRow row = register1.Student.NewRow(); //register1 is my DataSet
for (int i = 0; i<strArray.Length; i++)
{
row[i] = strArray[i];
}

register1.Student.Rows.Add(row);

register1.WriteXml(<<my filepath>>);




Although this code will add rows to my .xml file, I end up with the new information in the wrong place.

For example, let's assume that I want to add "<Student StudentID="2" Name="New Person" />" to my file.

At the moment, I get: -


<Register xmlns="http//tempuri.org/XMLFile1.xsd">
<Students>
<Student StudentID="1" Name="Bob" />
</Students>
<Student StudentID="2" Name="New Person" />
<Courses>
<Course CourseID="001" CourseName="Maths" />
<Courses>
</Register>




What I actually want is: -

<Register xmlns="http//tempuri.org/XMLFile1.xsd">
<Students>
<Student StudentID="1" Name="Bob" />
<Student StudentID="2" Name="New Person" />
</Students>
<Courses>
<Course CourseID="001" CourseName="Maths" />
<Courses>
</Register>


Notice how the rows marked 'Student' are inside the 'Students' tags.



Does anybody out there have any idea what I might be doing wrong?



Any help would be greatly appreciated!! :) :) :)

oracleguy
07-14-2005, 09:55 PM
"Students" Would be the DataTable inside of the DataSet, just like "Courses" would be another one. You want to add more rows onto the DataTable and then it should put them in the right place.

MattyJim
07-18-2005, 02:32 PM
Thanks, oracleguy!


Should I add more rows like: -

DataRow row = DataSet.Tables["TableName"].Rows.AddNew();


...or is there a better way?



Thanks for your help.

oracleguy
07-18-2005, 05:58 PM
Yeah that'd probably work, but I haven't done any C# with DataSets in a little while so I'm not 100% sure.