私の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>
私の問題は、バインドしてテーブルに追加する方法がわからないことを意味します。私を助けてください。どうもありがとう。