|
分類:[C#]
xmlからデータを読み込んでListに詰め込みたいのですが、「.Add」するたびに最後の部分?だけが上書きされます。
xmlには、「name」と「men」があります。
これを最終的に特定の部分だけを取り出せるようにしたいです。下記のように取り出せるように。
***[0].Name
***[2].Men
ソースファイルを張ります。
【Form1.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.IO;
namespace testPro
{
public partial class Form1 : Form
{
private List<string> m_name = new List<string>();
private List<bool> m_men = new List<bool>();
private int m_count;
private testPro.ClassA m_now = null;
private DataSet m_statas = null;
private string m_initfilename = System.IO.Directory.GetCurrentDirectory() + "\\initaldata.xml";
private testPro.ClassB mov = null;
public Form1()
{
InitializeComponent();
this.m_now = new testPro.ClassA();
this.mov = new testPro.ClassB();
}
private void button1_Click(object sender, EventArgs e)
{
this.m_statas = new DataSet();
this.m_count = dataGridView1.Rows.Count;
List<string> name = new List<string>();
List<bool> men = new List<bool>();
name = m_name;
men = m_men;
DataTable dt = new DataTable();
dt.Columns.Add("count", typeof(System.String));
for (int i = 0; i < m_count; i++)
{
dt.Columns.Add("name" + (i + 1), typeof(System.String));
}
for (int i = 0; i < m_count; i++)
{
dt.Columns.Add("men" + (i + 1), typeof(System.String));
}
DataRow row = dt.NewRow();
row["count"] = m_count;
for (int i = 0; i < m_count; i++)
{
row["name" + (i + 1).ToString()] = this.m_name[i];
row["men" + (i + 1).ToString()] = this.m_men[i];
}
dt.Rows.Add(row);
this.m_statas.Tables.Add(dt);
this.m_statas.WriteXml(m_initfilename);
}
private void dataGridView1_DragDrop(object sender, DragEventArgs e)
{
foreach( string filePath in (string[])e.Data.GetData( DataFormats.FileDrop ) ) {
String str = Path.GetFileName( filePath );
this.dataGridView1.Rows.Add( System.IO.Path.GetFileNameWithoutExtension( str ) );
for( int i = 0; i < 1; i++ ) {
this.m_now.Name = str;
this.dataGridView1.Rows[this.m_count].Cells[1].Value = false;
this.m_now.Men = (bool)this.dataGridView1.Rows[m_count].Cells[1].Value;
this.dataGridView1.AllowUserToAddRows = false;
this.m_count = dataGridView1.Rows.Count;
this.m_name.Add(str);
this.m_men.Add( (bool)this.dataGridView1.Rows[i].Cells[1].Value );
}
for( int i = 0; i < m_count; i++ ) {
this.dataGridView1.Rows[i].Cells[0].Value = m_name[i];
this.dataGridView1.Rows[i].Cells[1].Value = m_men[i];
}
}
}
private void dataGridView1_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
e.Effect = DragDropEffects.Copy;
}
}
}
}
【ClassA.cs】
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace testPro
{
class ClassA
{
private string name;
private bool men;
public ClassA()
{
}
/// <summary> 名前 </summary>
public string Name {
set {
this.name = value;
}
get {
return this.name;
}
}
/// <summary> 男かどうか </summary>
public bool Men {
set {
this.men = value;
}
get {
return this.men;
}
}
}
}
【ClassB.cs】
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
namespace testPro
{
class ClassB
{
private DataSet m_statas = null;
private List<ClassA> total = new List<ClassA>();
private List<ClassA> enable = new List<ClassA>();
private List<ClassA> disable = new List<ClassA>();
private testPro.ClassA cA;
private string m_initfilename = System.IO.Directory.GetCurrentDirectory() + "\\initaldata.xml";
public ClassB() {
this.cA = new testPro.ClassA();
this.m_loadMask();
}
/// <summary> 全て </summary>
public List<ClassA> Total
{
get {
return this.total;
}
}
/// <summary> ON </summary>
public List<ClassA> Enable
{
get {
return this.enable;
}
}
/// <summary> OFF </summary>
public List<ClassA> Disable
{
get {
return this.disable;
}
}
private void m_loadMask() {
if( System.IO.File.Exists( this.m_initfilename ) ) {
this.m_statas = new DataSet();
this.m_statas.ReadXml( this.m_initfilename );
int totalcount = Convert.ToInt32(this.m_statas.Tables[0].Rows[0]["count"] );
for (int i = 0; i < totalcount; i++)
{
cA.Name = this.m_statas.Tables[0].Rows[0]["name" + (1 + i)].ToString();
cA.Men = Convert.ToBoolean(this.m_statas.Tables[0].Rows[0]["men" + (1 + i)]);
total.Add(cA);
}
}
}
}
}
|