Matt

Matt

  • NA
  • 4
  • 0

Problems when adding new rows to an .xml file (using C#)

Jul 12 2005 8:24 AM

Hi! 

 

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


The code I'm presently using 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>




...when 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 in the correct version.



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



Any help would be greatly appreciated!!


Answers (2)