绑定多个列表xml tp dataGridView

时间:2013-10-18 15:30:38

标签: c# xml list datagridview

我的xml结构是

    <?xml version="1.0" encoding="utf-8"?>
    <tasks>
      <task>
        <taskId>1</taskId>
        <taskTitle>Viet Nam</taskTitle>
        <taskDesc>Take survey about Microsoft advertisement</taskDesc>
        <taskPerson>Young Women</taskPerson>
        <taskLong>2.4</taskLong>
        <taskLat>1.5</taskLat>
        <taskTime>2010-01-08</taskTime>
        <taskQuestions>

          <taskQuestion>
            <taskQuestionId>1</taskQuestionId>
            <taskQuestionType>1</taskQuestionType>
            <taskQuestionTitle>Single Choice Survey</taskQuestionTitle>
            <taskQuestionDesc>What is the name of brand that you have just watching in the video clip?</taskQuestionDesc>
            <taskQuestionAnswers>
              <taskQuestionAnswer>Microsoft</taskQuestionAnswer>
              <taskQuestionAnswer>Nokia Vietnam</taskQuestionAnswer>
              <taskQuestionAnswer>Samsung</taskQuestionAnswer>
              <taskQuestionAnswer>China Telecom</taskQuestionAnswer>
            </taskQuestionAnswers>
            <taskAnswers>
              <taskAnswer>A</taskAnswer>
              <taskAnswer>B</taskAnswer>
            </taskAnswers>
          </taskQuestion>
        </taskQuestions>
      </task>

    </tasks>

我的Form.cs文件是

            using System;
            using System.Collections.Generic;
            using System.ComponentModel;
            using System.Data;
            using System.Drawing;
            using System.Linq;
            using System.Text;
            using System.Threading.Tasks;
            using System.Windows.Forms;
            using System.Xml;
            using System.Xml.Linq;

            namespace test
            {

                public partial class Form1 : Form
                {
                    public class TaskModel
                    {
                        public string TaskId { get; set; }
                        public string TaskTitle { get; set; }
                        public string TaskDesc { get; set; }
                        public double TaskLong { get; set; }
                        public double TaskLat { get; set; }
                        public DateTime TaskTime { get; set; }
                        public string TaskQuestionId { get; set; }
                        public string TaskQuestionType { get; set; }
                        public string TaskQuestionTitle { get; set; }
                        public string TaskQuestionDesc { get; set; }
                        public List<TaskQuestionAnswers> TaskQuestionAnswers { get; set; }

                    }

                    public class TaskQuestionAnswers
                    {
                        public string TaskQuestionAnswer;
                    }
                    private BindingList<TaskModel> tasks;
                    private BindingList<TaskQuestionAnswers> listQuest = new BindingList<TaskQuestionAnswers>();


                    public Form1()
                    {
                        InitializeComponent();
                        tasks = new BindingList<TaskModel>();
                        listQuest = new BindingList<TaskQuestionAnswers>();
                        updateGridView1();
                    }

                    private void updateGridView1()
                    {
                        dataGridView1.DataSource = null;
                        dataGridView1.DataSource = tasks;
                        //DataGridViewColumn col = new DataGridViewTextBoxColumn();
                        //col.HeaderText = "Hi there";
                        //int colIndex = dataGridView1.Columns.Add(col);
                    }


                    private void button1_Click(object sender, EventArgs e)
                    {
                        listQuest = new BindingList<TaskQuestionAnswers>();

                        OpenFileDialog openFileDialog1 = new OpenFileDialog();
                        DialogResult result = openFileDialog1.ShowDialog();
                        if (result == DialogResult.OK)
                        {
                            string file = openFileDialog1.FileName;

                            try
                            {

                                TaskModel tmp = new TaskModel();
                                XmlDocument xmlDoc = new XmlDocument();
                                xmlDoc.Load(file);
                                var xml = XElement.Parse(xmlDoc.InnerXml);
                                var jobs = xml.Descendants("taskQuestion").ToList();
                                var taskElement = xml.Descendants("task").ToList();
                                var data = from query in xml.Descendants("taskQuestionAnswer")
                                           select new TaskQuestionAnswers
                                           {
                                               TaskQuestionAnswer = query.Value
                                           };

                                var taskId = xml.Element("task").Elements("taskId").First().Value;
                                foreach (var taskE in taskElement)
                                {
                                    foreach (var job in jobs)
                                    {
                                        tasks.Add(new TaskModel
                                        {
                                            TaskId = taskId,
                                            TaskTitle = taskE.Element("taskTitle").Value,
                                            TaskDesc = taskE.Element("taskDesc").Value,
                                            TaskLat = XmlConvert.ToDouble(taskE.Element("taskLat").Value),
                                            TaskLong = XmlConvert.ToDouble(taskE.Element("taskLong").Value),
                                            TaskQuestionId = job.Element("taskQuestionId").Value,
                                            TaskQuestionType = job.Element("taskQuestionType").Value,
                                            TaskTime = XmlConvert.ToDateTime(taskE.Element("taskTime").Value),
                                            TaskQuestionTitle = job.Element("taskQuestionTitle").Value,
                                            TaskQuestionAnswers = data.ToList(),
                                            TaskQuestionDesc = job.Element("taskQuestionDesc").Value,
                                        });
                                        //dataGridView1.DataSource = data;
                                    }
                                }
                            }
                            catch (Exception)
                            {
                                MessageBox.Show("Error");
                            }
                        }

                        //var taskTitle = xml.Element("task").Elements("taskTitle").Second().Value;
                        updateGridView1();
                    }
                }
            }

我尝试使用上述结构从文件XML绑定,并希望显示如下:

TaskId | TaskTitle | TaskDesc | TaskLong | TaskLat | TaskTime | TaskQuestionId | TaskQuestionType | TaskQuestionTitle | TaskQuestionDesc | TaskQuestionAnwser

输出结果如:

1 | Task of Title | Task of Desc | Task Long | Task lat | Task Time | Task Question ID | Task Question Type | TaskQuestionTitle | Task Question Desc | Task Question Answer1, Task Question Answer2

我只有TaskQuestionAnswer的列没有出现。我不知道绑定并将其插入到我的可用表中,其中包含从TaskId到TaskQuestionDesc的列。

            <taskQuestionAnswers>
              <taskQuestionAnswer>Microsoft</taskQuestionAnswer>
              <taskQuestionAnswer>Nokia Vietnam</taskQuestionAnswer>
              <taskQuestionAnswer>Samsung</taskQuestionAnswer>
              <taskQuestionAnswer>China Telecom</taskQuestionAnswer>
            </taskQuestionAnswers>

我的问题意味着我不知道如何绑定并添加到我的表中。请帮我。 非常感谢。

0 个答案:

没有答案