Click here to Skip to main content
15,889,909 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
This is my XML structure after serializing

XML
<?xml version="1.0"?>
    <ArrayOfTaskModel xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
      <TaskModel>
        <TaskID>Task ID 1</TaskID>
        <TaskDecs>Task Desc 1</TaskDecs>
        <TaskPerson>Task Person 1</TaskPerson>
        <TaskLong>Task Long 1</TaskLong>
        <TaskLat>Task lat 1</TaskLat>
        <TaskQuestions>
          <TaskQuestion>
            <TaskQuestionId>1</TaskQuestionId>
            <TaskQuestionType>Task Type 1</TaskQuestionType>
            <TaskQuestionTitle>Task Title 1</TaskQuestionTitle>
            <TaskQuestionDesc>Task Desc 1</TaskQuestionDesc>
            <TaskQuestionAnswers>
              <string>test</string>
              <string>test2</string>
            </TaskQuestionAnswers>
            <Answers>
              <string>test</string>
              <string>test2</string>
            </Answers>
          </TaskQuestion>
        </TaskQuestions>
      </TaskModel>
    </ArrayOfTaskModel>


Now I want to add all data from this xml file into my datagridview. I tried to use this code

XML
XmlSerializer xmlser = new XmlSerializer(typeof(List<TaskModel>));
StreamReader srdr = new StreamReader(@"D:\myfile.xml");
List<TaskModel> p = (List<TaskModel>)xmlser.Deserialize(srdr);
srdr.Close();
dataGridView1.DataSource = p;



With my TaskModel class is
C#
public class TaskModel
{
    public string TaskID { get; set; }
    public string TaskTitle { get; set; }
    public string TaskDecs { get; set; }
    public string TaskPerson { get; set; }
    public string TaskLong { get; set; }
    public string TaskLat { get; set; }
    public string TaskTime { get; set; }
    public List<TaskQuestion> TaskQuestions { get; set; }
}


And TaskQuestion.cs is
C#
public class TaskQuestion
{
    public string TaskQuestionId { get; set; }
    public string TaskQuestionType { get; set; }
    public string TaskQuestionTitle { get; set; }
    public string TaskQuestionDesc { get; set; }
    public List<string> TaskQuestionAnswers { get; set; }
    public List<string> Answers { get; set; }
}


VB
If I deserialize with the above code, I just get some element of TaskModel, except TaskQuestions because it's a list. How can I get it and bind to my datagridview? I want to display the following result into dataGridView, such as

TaskID | TaskTitle | Task Desc | TaskQuestionID | TaskQuestionType | TaskQuestionAnswers 
Please help me.
Posted

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