PDA

View Full Version : سوال: اضافه شدن فقط سطر آخر دیتاگریدویو داخل فایل Xml



forodo
پنج شنبه 22 خرداد 1393, 19:27 عصر
سلام
چرا فقط آخرین سطر دیتاگریدویو داخل فایل Xml ذخیره می شه؟
XmlDocument xdoc = new XmlDocument();
string xPath = @"ali.xml";

//گره های لازم را ایجاد می کنیم
XmlDeclaration declaration = xdoc.CreateXmlDeclaration("1.0", "UTF-8", "yes");
XmlComment comment = xdoc.CreateComment("This is an XML Generated File");
XmlElement root = xdoc.CreateElement("Persons");
XmlElement person = xdoc.CreateElement("Person");
XmlAttribute name = xdoc.CreateAttribute("name");
XmlElement age = xdoc.CreateElement("Age");

xdoc.AppendChild(declaration);
xdoc.AppendChild(comment);
xdoc.AppendChild(root);

//مقادیر را به گره ها اضافه می کنیم
for (int i = 0; i < dataGridView1.RowCount - 1; i++)
{
name.Value = dataGridView1.Rows[i].Cells[0].Value.ToString();
age.InnerText = dataGridView1.Rows[i].Cells[1].Value.ToString();
button1.Focus();

//سند را ایجاد می کنیم
root.AppendChild(person);
person.Attributes.Append(name);
person.AppendChild(age);

}

xdoc.Save(xPath);
من داخل دیتاگریدویو اینو می زنم.
1 1
2 2
که سطر آخر فقط ذخیره می شه!
http://up.iranfilm182.com/images/76850542867071181587.png

Mahmoud.Afrad
پنج شنبه 22 خرداد 1393, 20:28 عصر
به ازای هر شخص باید یک element جدید ایجاد کنی پس سه خط زیر رو ببر داخل حلقه:

XmlElement person = xdoc.CreateElement("Person");
XmlAttribute name = xdoc.CreateAttribute("name");
XmlElement age = xdoc.CreateElement("Age");

forodo
پنج شنبه 22 خرداد 1393, 21:25 عصر
این هم جواب ما که بقیه دوستان هم بدونند.
XmlDocument xdoc = new XmlDocument();
// تعریف مسیر فایل ایکس ام ال
string xPath = @"ali.xml";

// اگر فایل ایکس ام ال وجود نداشت
if (!File.Exists(xPath))
{
//گره های لازم را ایجاد می کنیم
XmlDeclaration declaration = xdoc.CreateXmlDeclaration("1.0", "UTF-8", "yes");
XmlComment comment = xdoc.CreateComment("This is an XML Generated File");
XmlElement root = xdoc.CreateElement("Persons");

xdoc.AppendChild(declaration);
xdoc.AppendChild(comment);
xdoc.AppendChild(root);

//مقادیر را به گره ها اضافه می کنیم
for (int i = 0; i < dataGridView1.RowCount - 1; i++)
{
XmlElement person = xdoc.CreateElement("Person");
XmlAttribute name = xdoc.CreateAttribute("name");
XmlElement age = xdoc.CreateElement("Age");
name.Value = dataGridView1.Rows[i].Cells[0].Value.ToString();
age.InnerText = dataGridView1.Rows[i].Cells[1].Value.ToString();
button1.Focus();
root.AppendChild(person);
person.Attributes.Append(name);
person.AppendChild(age);
}

xdoc.Save(xPath);
}
else //اگر فایل XML از قبل موجود بود
{
//را بارگذاری می کنیم XMLفایل
xdoc.Load(xPath);

//عنصر ریشه را بدست می آوریم
XmlElement root = xdoc.DocumentElement;

//مقادیر را به گره ها اضافه می کنیم
for (int i = 0; i < dataGridView1.RowCount - 1; i++)
{
XmlElement person = xdoc.CreateElement("Person");
XmlAttribute name = xdoc.CreateAttribute("name");
XmlElement age = xdoc.CreateElement("Age");
name.Value = dataGridView1.Rows[i].Cells[0].Value.ToString();
age.InnerText = dataGridView1.Rows[i].Cells[1].Value.ToString();
button1.Focus();
//را ایجاد می کنیم Personعنصر
person.Attributes.Append(name);
person.AppendChild(age);
// را به آخر عنصر ریشه اضافه می کنیم Personعنصر
root.AppendChild(person);

}
//Save the document
xdoc.Save(xPath);
}
این هم خروجیش:
http://up.iranfilm182.com/images/69070439300494884692.png