بفرما
برای ذخیره :
[CSHARP]
Pashmak.XmlDoc = new XmlDocument();
XmlElement XRootA = Pashmak.XmlDoc.CreateElement("Pashmak");
XmlElement XRootB = Pashmak.XmlDoc.CreateElement("Softafzar");
{
XmlElement XItems = Pashmak.XmlDoc.CreateElement("Items");
for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
{
try
{
XmlElement XItem = Pashmak.XmlDoc.CreateElement("Item");
XmlElement XCLM1 = Pashmak.XmlDoc.CreateElement("Column1");
XCLM1.InnerText = dataGridView1.Rows[i].Cells[0].Value.ToString();
XItem.AppendChild(XCLM1);
XItems.AppendChild(XItem);
}
catch { }
}
XRootB.AppendChild(XItems);
XRootA.AppendChild(XRootB);
}
Pashmak.XmlDoc.AppendChild(XRootA);
SaveFileDialog sd = new SaveFileDialog();
sd.Filter = "SoftAfzar Database (.db)|.db";
Random randomize = new Random();
sd.FileName = "SoftAfzar Database" + randomize.Next();
string filePath = "";
if (sd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
StreamWriter save = new StreamWriter(sd.FileName);
save.Write(Pashmak.XmlDoc.InnerXml);
filePath = sd.FileName;
label1.Text = "Database Saved on\n" + filePath;
save.Close();
}
[/CSHARP]
برای بازیابی
[CSHARP]
OpenFileDialog od = new OpenFileDialog();
od.Filter = "SoftAfzar Database (.db)|.db";
od.FileName = "SoftAfzar Database.db";
if (od.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
StreamReader loaddb = new StreamReader(od.FileName);
string data = loaddb.ReadToEnd();
loaddb.Close();
Pashmak.XmlDoc = new XmlDocument();
try
{
Pashmak.XmlDoc.LoadXml(data);
}
catch
{
return;
}
if (Pashmak.XmlDoc == null || Pashmak.XmlDoc.InnerXml == "")
return;
XmlNode XmlRoot = Pashmak.XmlDoc.GetElementsByTagName("Pashmak")[0];
for (int i = 0; i < XmlRoot.ChildNodes.Count; i++)
{
XmlNode XmlRChild = XmlRoot.ChildNodes[i];
switch (XmlRChild.Name)
{
case "Softafzar":
if (XmlRChild.SelectSingleNode("//Pashmak/Softafzar/Items") != null &&
XmlRChild.SelectSingleNode("//Pashmak/Softafzar/Items/Item") != null)
{
XmlNodeList ItemsList = XmlRChild.SelectNodes("//Pashmak/Softafzar/Items/Item");
for (int j = 0; j < ItemsList.Count; j++)
{
string A = "";
for (int t = 0; t < ItemsList[j].ChildNodes.Count; t++)
{
if (ItemsList[j].ChildNodes[t].Name == "Column1")
A = ItemsList[j].ChildNodes[t].InnerText;
}
dataGridView1.Rows.Add(A);
}
}
break;
}
}
}
[/CSHARP]