Click here to Skip to main content
15,896,207 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have this XML file structure:

HTML
<StudentsSystem>
   <Students StudentId="">
    <FirstName></FirstName>
    <LastName></LastName>
    <Gender></Gender>
    <BirthDate></BirthDate>
    <HomeCity></HomeCity>
    <Address></Address>
    <Class></Class>
    <StudiesYear></StudiesYear>
  </Students>

  <Tutors TutorID="">
    <FirstName></FirstName>
    <LastName></LastName>
    <NickName></NickName>
    <Gender></Gender>
    <BirthDate></BirthDate>
    <HomeCity></HomeCity>
    <Address></Address>
    <Mobile></Mobile>
    <Email></Email>
    <Class></Class>
    <Subject></Subject>
  </Tutors>
  

</StudentsSystem>


and I want to bind Tutors to gridview,
I tried this:
C#
using (DataSet ds = new DataSet())
    {
        ds.ReadXml(Server.MapPath("~/StudentsSystem.xml"));
        GridView1.DataSource = ds;
        GridView1.DataBind();
    }


But this's wrong and get the whole XML file..
What could I write?!
Posted

1 solution

The parsing will create 2 datatables in the dataset. One for Students and one for Tutors.
If you want to bind the Tutors to the gridview, you can do it on one of the following ways.

C#
using (DataSet ds = new DataSet())
    {
        ds.ReadXml(Server.MapPath("~/StudentsSystem.xml"));
        GridView1.DataSource = ds.Tables[1];//Tutors is the second table and it is zero based index
        GridView1.DataBind();
    }

OR
C#
using (DataSet ds = new DataSet())
    {
        ds.ReadXml(Server.MapPath(&quot;~/StudentsSystem.xml&quot;));
        GridView1.DataSource = ds.Tables["Tutors"];
        GridView1.DataBind();
    }


I would suggest the second one as it is easy to identify using table names.
 
Share this answer
 
v2
Comments
Y.Ahmad 19-Jun-15 2:58am    
ammmm
Thank you
I noticed that, when I add new row, I can't find this row in the XML file..
I work with sedna, and Sedna saves the changes in its Database..
How can I save the record in the XML file?!
This's my code:
<pre lang="c#">
string sGender = "Male";
if (RBFemale.Checked)
sGender="Female";


string sQuery = "update insert <Tutors TutorID='"+Guid.NewGuid().ToString()+"'>" +
"<FirstName>"+TxtFirstName.Text+"</FirstName>"+
"<LastName>"+TxtLastName.Text+"</LastName>"+
"<NickName>"+TxtNickName.Text+"</NickName>"+
"<Gender>"+sGender+"</Gender>"+
"<BirthDate>"+txtBirthDate.Text+"</BirthDate>"+
"<HomeCity>"+txtHomeCity.Text+"</HomeCity>"+
"<Address>"+TxtAddress.Text+"</Address>"+
"<Mobile>"+TxtMobile.Text+"</Mobile>"+
"<Email>"+TxtEmail.Text+"</Email>"+
"<Class>"+txtClass.Text+"</Class>"+
"<Subject>"+txtSubject.Text+"</Subject>"+
"into document('StudentSystem')/Users";


SednaSession s = new SednaSession();

s.Start("localhost", SednaSession.DEFAULT_SERVER_PORT, "StudentSystem", "SYSTEM", "MANAGER");

SednaQueryResults sql = s.Execute(sQuery);

if (s.HasTransaction)
{
s.CommitTransaction();

lblStatus.Text = "Tutor has been added successfully";
s.Close();
}
else
{
lblStatus.Text = "error!";
s.Close();
}
</pre>
Mathi Mani 19-Jun-15 12:53pm    
Refer this link http://stackoverflow.com/questions/8398031/how-to-insert-data-into-an-existing-xml-file-in-asp-net for inserting data into an existing XML

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900