-2
從用戶在文本文件中我存儲的數據的分隔符,這是我的代碼做這個如何使用c#在XML文件中存儲來自txt文件的數據?使用
Student std = new Student();
dataGridView1.Rows.Clear(); //to clear the dataGridView Before showing the data
dataGridView1.Refresh();
List<Student> students = new List<Student>();
using (StreamReader sr = new StreamReader(txt_path.Text))
{
int x = 0;
while (sr.Peek() >= 0)
{
string str;
string[] strArray;
str = sr.ReadLine();
strArray = str.Split('@', '#');
Student s = new Student();
s.ID = int.Parse(strArray[0]);
s.Name = strArray[1];
s.Address = strArray[2];
s.Phone = strArray[3];
DataGridViewRow row = new DataGridViewRow();
row.CreateCells(dataGridView1); // this line was missing
row.Cells[x].Value = s.ID;
row.Cells[++x].Value = s.Name;
row.Cells[++x].Value = s.Address;
row.Cells[++x].Value = s.Phone;
dataGridView1.Rows.Add(row);
students.Add(s);
x = 0;
}
}
這裏的問題,畢竟我的數據存儲在文本文件,然後我想保持它像XML文件「中圖片「。
我試過,但我在這張照片未能像
這是我做的Seconde系列圖片
try
{
List<Student> students = new List<Student>();
using (StreamReader sr = new StreamReader(txt_path.Text))
{
string xmlc = string.Empty;
while (sr.Peek() >= 0)
{
string str;
string[] strArray;
str = sr.ReadLine();
if (!string.IsNullOrWhiteSpace(str) && !str.StartsWith("#"))
{
xmlc += str;
strArray = xmlc.Split('@', '#');
saveXml.saveData(xmlc, "data.xml");
saveXml.saveData(strArray, "data.xml");
}
Student s = new Student();
s.ID = int.Parse(strArray[0]);
s.Name = strArray[1];
s.Address = strArray[2];
s.Phone = strArray[3];
students.Add(s);
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
你能告訴我們用於創建/寫入'Xml'的代碼嗎? –
@HariPrasad我編輯了我的問題。 –
@FelicePollano,但在dataGridview和文本文件中存儲數據 –