ذخیره و بازیابی تمامی اطلاعات در XML سی پلاس پلاس دات نت

بازدید34.8kپست ها6آخرین فعالیت10 سال پیش
0
0

درود به کاربران ، مدیران و همراهان گرامی سافت افزار،

در اینجا قرار است که با یاری همدیگر ، کدهای ذخیره ی اطلاعات در فایل XML را در زبان سی پلاس پلاس بر پایه دات نت آموزش دهیم.

چنانچه سوالی برای شما دوستان گرامی پیش آمد ، خواهشمندیم در تاپیک جداگانه آنرا بپرسید.

سپاس گذارم.

usingـه مورد نیاز در کل این پروژه ها ،این ها میباشند :

using namespace System::Xml

	using namespace System::IO;

در قسمت Generalـه فرمتان کد زیر را بنویسید :

public: XmlDocument^ XmlDoc;

منبع : سافت افزار

کدنویس : رامتین جوکار

کپی برداری از کدهای پایین فقط با ذکر نام سافت افزار قانونی و مجاز میباشد.

0

برای ذخیره ی TextBox ها کد زیر را به کار ببرید :

XmlDoc = gcnew XmlDocument();

            XmlElement^ XRootA = XmlDoc->CreateElement("TextBoxes");
            XmlElement^ txtbox1 = XmlDoc->CreateElement("textBox1");
            txtbox1->InnerText = textBox1->Text;

            XmlElement^ txtbox2 = XmlDoc->CreateElement("textBox2");
            txtbox2->InnerText = textBox2->Text;

            XRootA->AppendChild(txtbox1);
            XRootA->AppendChild(txtbox2);

            XmlDoc->AppendChild(XRootA);

			SaveFileDialog^  sd = gcnew SaveFileDialog();
            sd->Filter = "SoftAfzar Database (*.db)|*.db";
            Random^  randomize = gcnew Random();
            sd->FileName = "SoftAfzar Database" + randomize->Next();
            if (sd->ShowDialog() == System::Windows::Forms::DialogResult::OK)
            {
                StreamWriter^ save = gcnew StreamWriter(sd->FileName);
                save->Write(XmlDoc->InnerXml);
                save->Close();
            }

و همچنین برای بازیابی اطلاعات آن از کد زیر استفاده نمایید :

OpenFileDialog^ od = gcnew OpenFileDialog();
            od->Filter = "SoftAfzar Database (*.db)|*.db";
            od->FileName = "SoftAfzar Database.db";
            if (od->ShowDialog() == System::Windows::Forms::DialogResult::OK)
            {
                StreamReader^ loaddb = gcnew StreamReader(od->FileName);
                String^ data = loaddb->ReadToEnd();
                loaddb->Close();
                XmlDoc = gcnew XmlDocument();
                try
                {
                    XmlDoc->LoadXml(data);
                }
                catch(Exception^ ex)
                {
                    return;
                }
                XmlNodeList^ XnL = XmlDoc->ChildNodes;
				for(int i = 0; i < XmlDoc->ChildNodes->Count;i++)
				{
					if(XnL[i]->Name == "TextBoxes")
					{
						XmlNodeList^ XnL2 = XnL[i]->ChildNodes;
						for (int j = 0; j < XnL2->Count; j++)
                        {
							if(XnL2[j]->Name == "textBox1")
								 textBox1->Text = XnL2[j]->InnerText;
	
							else if(XnL2[j]->Name == "textBox2")
								 textBox2->Text = XnL2[j]->InnerText;
						}
					}
				}
			}
0

برای ذخیره ی RadioButton ها کد زیر را به کار ببرید :

XmlDoc = gcnew XmlDocument();
            XmlElement^ XRootA = XmlDoc->CreateElement("RadioButton");

            XmlElement^ radiobtn1 = XmlDoc->CreateElement("radioButton1");
            radiobtn1->InnerText = radioButton1->Checked.ToString();

            XmlElement^ radiobtn2 = XmlDoc->CreateElement("radioButton2");
            radiobtn2->InnerText = radioButton2->Checked.ToString();

            XRootA->AppendChild(radiobtn1);
            XRootA->AppendChild(radiobtn2);

            XmlDoc->AppendChild(XRootA);

            SaveFileDialog^ sd = gcnew SaveFileDialog();
            sd->Filter = "SoftAfzar Database (*.db)|*.db";
            Random^ randomize = gcnew Random();
            sd->FileName = "SoftAfzar Database" + randomize->Next();
            if (sd->ShowDialog() == System::Windows::Forms::DialogResult::OK)
            {
                StreamWriter^ save = gcnew StreamWriter(sd->FileName);
                save->Write(XmlDoc->InnerXml);
                save->Close();
            }

و همچنین برای بازیابی اطلاعات آن از کد زیر استفاده نمایید :

OpenFileDialog^ od = gcnew OpenFileDialog();
            od->Filter = "SoftAfzar Database (*.db)|*.db";
            od->FileName = "SoftAfzar Database.db";
            if (od->ShowDialog() == System::Windows::Forms::DialogResult::OK)
            {
                StreamReader^ loaddb = gcnew StreamReader(od->FileName);
                String^ data = loaddb->ReadToEnd();
                loaddb->Close();
                XmlDoc = gcnew XmlDocument();
                try
                {
                    XmlDoc->LoadXml(data);
                }
                catch(Exception^ ex)
                {
                    return;
                }
                XmlNodeList^ XnL = XmlDoc->ChildNodes;
                for (int i = 0; i < XmlDoc->ChildNodes->Count; i++)
                {
					if(XnL[i]->Name == "RadioButton")
					{
						XmlNodeList^ XnL2 = XnL[i]->ChildNodes;
						for (int j = 0; j < XnL2->Count; j++)
                        {
							if(XnL2[j]->Name == "radioButton1")
						    radioButton1->Checked = bool::Parse(XnL2[j]->InnerText);
	
							else if(XnL2[j]->Name == "radioButton2")
								radioButton2->Checked = bool::Parse(XnL2[j]->InnerText);
						}
					}
                }
            }
0

برای ذخیره ی CheckBoxها کد زیر را به کار ببرید :

XmlDoc = gcnew XmlDocument();
            XmlElement^ XRootA = XmlDoc->CreateElement("CheckBoxes");

            XmlElement^ chbox1 = XmlDoc->CreateElement("checkBox1");
            chbox1->InnerText = checkBox1->Checked.ToString();

            XmlElement^ chbox2 = XmlDoc->CreateElement("checkBox2");
            chbox2->InnerText = checkBox2->Checked.ToString();

            XRootA->AppendChild(chbox1);
            XRootA->AppendChild(chbox2);

            XmlDoc->AppendChild(XRootA);

            SaveFileDialog^ sd = gcnew SaveFileDialog();
            sd->Filter = "SoftAfzar Database (*.db)|*.db";
            Random^ randomize = gcnew Random();
            sd->FileName = "SoftAfzar Database" + randomize->Next();
            if (sd->ShowDialog() == System::Windows::Forms::DialogResult::OK)
            {
                StreamWriter^ save = gcnew StreamWriter(sd->FileName);
                save->Write(XmlDoc->InnerXml);
                save->Close();
            }

و همچنین برای بازیابی اطلاعات آن از کد زیر استفاده نمایید :

OpenFileDialog^ od = gcnew OpenFileDialog();
            od->Filter = "SoftAfzar Database (*.db)|*.db";
            od->FileName = "SoftAfzar Database.db";
            if (od->ShowDialog() == System::Windows::Forms::DialogResult::OK)
            {
                StreamReader^ loaddb = gcnew StreamReader(od->FileName);
                String^ data = loaddb->ReadToEnd();
                loaddb->Close();
                XmlDoc = gcnew XmlDocument();
                try
                {
                    XmlDoc->LoadXml(data);
                }
                catch(Exception^ ex)
                {
                    return;
                }
                XmlNodeList^ XnL = XmlDoc->ChildNodes;
                for (int i = 0; i < XmlDoc->ChildNodes->Count; i++)
                {
					if(XnL[i]->Name == "CheckBoxes")
					{
						XmlNodeList^ XnL2 = XnL[i]->ChildNodes;
						for (int j = 0; j < XnL2->Count; j++)
                        {
							if(XnL2[j]->Name == "checkBox1")
						    checkBox1->Checked = bool::Parse(XnL2[j]->InnerText);
	
							else if(XnL2[j]->Name == "checkBox2")
								checkBox2->Checked = bool::Parse(XnL2[j]->InnerText);
						}
					}
                }
            }
0

برای ذخیره ی ListBoxها کد زیر را به کار ببرید :

XmlDoc = gcnew XmlDocument();
            XmlElement^ listBoxP = XmlDoc->CreateElement("ListBoxes");
            {
                for (int i = 0; i < listBox1->Items->Count; i++)
                {
                    XmlElement^ Item = XmlDoc->CreateElement("Item");
                    Item->InnerText = listBox1->Items[i]->ToString();
                    listBoxP->AppendChild(Item);
                }
            }
            XmlDoc->AppendChild(listBoxP);

            SaveFileDialog^ sd = gcnew SaveFileDialog();
            sd->Filter = "SoftAfzar Database (*.db)|*.db";
            Random^ randomize = gcnew Random();
            sd->FileName = "SoftAfzar Database" + randomize->Next();
            if (sd->ShowDialog() == System::Windows::Forms::DialogResult::OK)
            {
                StreamWriter^ save = gcnew StreamWriter(sd->FileName);
                save->Write(XmlDoc->InnerXml);
                save->Close();
            }

و همچنین برای بازیابی اطلاعات آن از کد زیر استفاده نمایید :

OpenFileDialog^ od = gcnew OpenFileDialog();
            od->Filter = "SoftAfzar Database (*.db)|*.db";
            od->FileName = "SoftAfzar Database.db";
            if (od->ShowDialog() == System::Windows::Forms::DialogResult::OK)
            {
                StreamReader^ loaddb = gcnew StreamReader(od->FileName);
                String^ data = loaddb->ReadToEnd();
                loaddb->Close();
                XmlDoc = gcnew XmlDocument();
                try
                {
                    XmlDoc->LoadXml(data);
                }
                catch(Exception^ ex)
                {
                    return;
                }
                XmlNodeList^ XnL = XmlDoc->ChildNodes;
                for (int i = 0; i < XmlDoc->ChildNodes->Count; i++)
                {
					if(XnL[i]->Name == "ListBoxes")
					{
						XmlNodeList^ XnL2 = XnL[i]->ChildNodes;
						for (int j = 0; j < XnL2->Count; j++)
                        {
							if(XnL2[j]->Name == "Item")
						      listBox1->Items->Add(XnL2[j]->InnerText);
						}
					}
                }
            }
0

رای ذخیره ی DataGridView ها کد زیر را به کار ببرید :

XmlDoc = gcnew XmlDocument();
            XmlElement^ XRootA = XmlDoc->CreateElement("Pashmak");
            XmlElement^ XRootB = XmlDoc->CreateElement("Softafzar");
            {
                XmlElement^ XItems = XmlDoc->CreateElement("Items");
                for (int i = 0; i < dataGridView1->Rows->Count - 1; i++)
                {
                    XmlElement^ XItem = XmlDoc->CreateElement("Item");
                    XmlElement^ XCLM1 = XmlDoc->CreateElement("Column1");
                    XmlElement^ XCLM2 = XmlDoc->CreateElement("Column2");
                    XCLM1->InnerText = dataGridView1->Rows[i]->Cells[0]->Value->ToString();
                    XCLM2->InnerText = dataGridView1->Rows[i]->Cells[1]->Value->ToString();
                    XItem->AppendChild(XCLM1);
                    XItem->AppendChild(XCLM2);
                    XItems->AppendChild(XItem);

                }
                XRootB->AppendChild(XItems);
                XRootA->AppendChild(XRootB);
            }
            XmlDoc->AppendChild(XRootA);
            SaveFileDialog^ sd = gcnew SaveFileDialog();
            sd->Filter = "SoftAfzar Database (*.db)|*.db";
            Random^ randomize = gcnew Random();
            sd->FileName = "SoftAfzar Database" + randomize->Next();
            String^ filePath = "";
            if (sd->ShowDialog() == System::Windows::Forms::DialogResult::OK)
            {
                StreamWriter^ save = gcnew StreamWriter(sd->FileName);
                save->Write(XmlDoc->InnerXml);
                filePath = sd->FileName;
                label1->Text = "Database Saved on\n" + filePath;
                save->Close();
            }

و همچنین برای بازیابی اطلاعات آن از کد زیر استفاده نمایید :

OpenFileDialog^ od = gcnew OpenFileDialog();
            od->Filter = "SoftAfzar Database (*.db)|*.db";
            od->FileName = "SoftAfzar Database.db";
            if (od->ShowDialog() == System::Windows::Forms::DialogResult::OK)
            {
                StreamReader^ loaddb = gcnew StreamReader(od->FileName);
                String^ data = loaddb->ReadToEnd();
                loaddb->Close();
                XmlDoc = gcnew XmlDocument();
                try
                {
                    XmlDoc->LoadXml(data);
                }
                catch(Exception^ ex)
                {
                    return;
                }
				XmlNodeList^ XnL = XmlDoc->ChildNodes;
                for (int i = 0; i < XmlDoc->ChildNodes->Count; i++)
                {
					if(XnL[i]->Name == "Pashmak")
					{
						XmlNodeList^ XnL2 = XnL[i]->ChildNodes;
						for (int j = 0; j < XnL2->Count; j++)
                        {
							if(XnL2[j]->Name == "Softafzar")
							{
								XmlNodeList^ XnL3 = XnL2[j]->ChildNodes;
								for(int t = 0; t <  XnL3->Count;t++)
								{
									if(XnL3[t]->Name == "Items")
						          	{
										XmlNodeList^ XnL4 = XnL3[t]->ChildNodes;
										for(int b = 0; b <  XnL4->Count;b++)
								        {
											if(XnL4[t]->Name == "Item")
											{
												XmlNodeList^ XnL5 = XnL4[b]->ChildNodes;
												String^ A = ""; String^ B = "";
												for(int n = 0; n<XnL5->Count; n++)
												{
													if (XnL5[n]->Name == "Column1")
														A = XnL5[n]->InnerText;
													else if (XnL5[n]->Name == "Column2")
														B = XnL5[n]->InnerText;
												}
												dataGridView1->Rows->Add(A,B);	
											}
											
										}
									}
								}
							}
						}
					}
                }
            }
0

برای ذخیره سازی تنظیمات در یک فایل واحد از کدهای زیر استفاده نمایید :

XmlDoc = gcnew XmlDocument();
			XmlElement^ XRootA = XmlDoc->CreateElement("Pashmak");
            XmlElement^ XRootB = XmlDoc->CreateElement("Softafzar");
            {
                XmlElement^ XItems = XmlDoc->CreateElement("Items");
                for (int i = 0; i < dataGridView1->Rows->Count - 1; i++)
                {
                    XmlElement^ XItem = XmlDoc->CreateElement("Item");
                    XmlElement^ XCLM1 = XmlDoc->CreateElement("Column1");
                    XmlElement^ XCLM2 = XmlDoc->CreateElement("Column2");
                    XCLM1->InnerText = dataGridView1->Rows[i]->Cells[0]->Value->ToString();
                    XCLM2->InnerText = dataGridView1->Rows[i]->Cells[1]->Value->ToString();
                    XItem->AppendChild(XCLM1);
                    XItem->AppendChild(XCLM2);
                    XItems->AppendChild(XItem);

                }
                XRootB->AppendChild(XItems);
                XRootA->AppendChild(XRootB);
            }

			XmlElement^ listBoxP = XmlDoc->CreateElement("ListBoxes");
            {
                for (int i = 0; i < listBox1->Items->Count; i++)
                {
                    XmlElement^ Item = XmlDoc->CreateElement("Item");
                    Item->InnerText = listBox1->Items[i]->ToString();
                    listBoxP->AppendChild(Item);
                }
				XRootA->AppendChild(listBoxP);
            }


            XmlElement^ XRootC = XmlDoc->CreateElement("TextBoxes");
            {
                XmlElement^ txtbox1 = XmlDoc->CreateElement("textBox1");
                txtbox1->InnerText = textBox1->Text;
                XmlElement^ txtbox2 = XmlDoc->CreateElement("textBox2");
                txtbox2->InnerText = textBox2->Text;

                XRootC->AppendChild(txtbox1);
                XRootC->AppendChild(txtbox2);
                XRootA->AppendChild(XRootC);
            }

			XmlElement^ XRootD = XmlDoc->CreateElement("RadioButton");
            {
                XmlElement^ radiobtn1 = XmlDoc->CreateElement("radioButton1");
                radiobtn1->InnerText = radioButton1->Checked.ToString();
                XmlElement^ radiobtn2 = XmlDoc->CreateElement("radioButton2");
                radiobtn2->InnerText = radioButton2->Checked.ToString();

                XRootD->AppendChild(radiobtn1);
                XRootD->AppendChild(radiobtn2);
                XRootA->AppendChild(XRootD);
			}

			XmlElement^ XRootE = XmlDoc->CreateElement("CheckBoxes");
            {
                XmlElement^ chbox1 = XmlDoc->CreateElement("checkBox1");
                chbox1->InnerText = checkBox1->Checked.ToString();
                XmlElement^ chbox2 = XmlDoc->CreateElement("checkBox2");
                chbox2->InnerText = checkBox2->Checked.ToString();

                XRootE->AppendChild(chbox1);
                XRootE->AppendChild(chbox2);
                XRootA->AppendChild(XRootE);
            }
			XmlDoc->AppendChild(XRootA);
			SaveFileDialog^ sd = gcnew SaveFileDialog();
            sd->Filter = "SoftAfzar Database (*.db)|*.db";
            Random^ randomize = gcnew Random();
            sd->FileName = "SoftAfzar Database" + randomize->Next();
            String^ filePath = "";
            if (sd->ShowDialog() == System::Windows::Forms::DialogResult::OK)
            {
                StreamWriter^ save = gcnew StreamWriter(sd->FileName);
                save->Write(XmlDoc->InnerXml);
                filePath = sd->FileName;
                label1->Text = "Database Saved on\n" + filePath;
                save->Close();
            }

همچنین برای بازیابی آنها ، از کد زیر استفاده کنید :

OpenFileDialog^ od = gcnew OpenFileDialog();
            od->Filter = "SoftAfzar Database (*.db)|*.db";
            od->FileName = "SoftAfzar Database.db";
            if (od->ShowDialog() == System::Windows::Forms::DialogResult::OK)
            {
                StreamReader^ loaddb = gcnew StreamReader(od->FileName);
                String^ data = loaddb->ReadToEnd();
                loaddb->Close();
                XmlDoc = gcnew XmlDocument();
                try
                {
                    XmlDoc->LoadXml(data);
                }
                catch(Exception^ ex)
                {
                    return;
                }
				XmlNodeList^ XnL = XmlDoc->ChildNodes;
				for (int i = 0; i < XmlDoc->ChildNodes->Count; i++)
                {
					if(XnL[i]->Name == "Pashmak")
					{
						XmlNodeList^ XnL2 = XnL[i]->ChildNodes;
						for (int j = 0; j < XnL2->Count; j++)
                        {
							if(XnL2[j]->Name == "Softafzar")
							{
								XmlNodeList^ XnL3 = XnL2[j]->ChildNodes;
								for(int t = 0; t <  XnL3->Count;t++)
								{
									if(XnL3[t]->Name == "Items")
						          	{
										XmlNodeList^ XnL4 = XnL3[t]->ChildNodes;
										for(int b = 0; b <  XnL4->Count;b++)
								        {
											if(XnL4[t]->Name == "Item")
											{
												XmlNodeList^ XnL5 = XnL4[b]->ChildNodes;
												String^ A = ""; String^ B = "";
												for(int n = 0; n<XnL5->Count; n++)
												{
													if (XnL5[n]->Name == "Column1")
														A = XnL5[n]->InnerText;
													else if (XnL5[n]->Name == "Column2")
														B = XnL5[n]->InnerText;
												}
												dataGridView1->Rows->Add(A,B);	
											}
											
										}
									}
								}
							}
							else if(XnL2[j]->Name == "ListBoxes")
							{
							    XmlNodeList^ XnL3 = XnL2[j]->ChildNodes;
								for(int t = 0; t <  XnL3->Count;t++)
								{
									if(XnL3[t]->Name == "Item")
										listBox1->Items->Add(XnL3[t]->InnerText);
									
								}
							}
						    else if(XnL2[j]->Name == "TextBoxes")
							{
								XmlNodeList^ XnL3 = XnL2[j]->ChildNodes;
								for(int t = 0; t <  XnL3->Count;t++)
								{
									if(XnL3[t]->Name == "textBox1")
										textBox1->Text = XnL3[t]->InnerText;
									else if(XnL3[t]->Name == "textBox2")
										textBox2->Text = XnL3[t]->InnerText;
									
								}
							}
							else if(XnL2[j]->Name == "RadioButton")
							{
								XmlNodeList^ XnL3 = XnL2[j]->ChildNodes;
								for(int t = 0; t <  XnL3->Count;t++)
								{
									if(XnL3[t]->Name == "radioButton1")
									 radioButton1->Checked = bool::Parse(XnL3[t]->InnerText);
									else if(XnL3[t]->Name == "radioButton2")
										 radioButton2->Checked = bool::Parse(XnL3[t]->InnerText);
									
								}
							}
							else if(XnL2[j]->Name == "CheckBoxes")
							{
								XmlNodeList^ XnL3 = XnL2[j]->ChildNodes;
								for(int t = 0; t <  XnL3->Count;t++)
								{
									if(XnL3[t]->Name == "checkBox1")
									 checkBox1->Checked = bool::Parse(XnL3[t]->InnerText);
									else if(XnL3[t]->Name == "checkBox2")
										 checkBox2->Checked = bool::Parse(XnL3[t]->InnerText);
									
								}
							}

						}
					}
				}
			}

منبع : سافت افزار

کدنویس : رامتین جوکار
کپی برداری از کدهای بالا فقط با ذکر نام سافت افزار قانونی و مجاز میباشد.

سوال برنامه نویسی دارید؟

ندونستن عیب نیست، نپرسیدن چرا!

خوش آمدید

برای طرح سوال، ایجاد بحث و فعالیت در سایت نیاز است ابتدا وارد حساب کاربری خود شوید. در صورتی که هنوز عضو سایت نیستید میتوانید در عرض تنها چند ثانیه ثبت نام کنید.