如何编写和读取xml文件的数据,以便在c#中保存windows窗体的数据 [英] How Do I write and read data of xml file for save the data of windows form in c#

查看:167
本文介绍了如何编写和读取xml文件的数据,以便在c#中保存windows窗体的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Windows窗体包含datagridview(有学生信息)和菜单(有项目(保存,打开)

i想要那个


$ b填写所有学生信息后$ b



- >如果用户点击保存按钮

保存为窗口将打开

- >然后用户输入文件的名称,文件将保存(文件的扩展名可以是任何东西..例如.sgc)

--->然后所有的数据用户输入的datagridview将保存在该文件上

与想要打开该文件相同..



请帮忙.. !!这是代码..





  private   void  saveAsToolStripMenuItem_Click( object  sender,EventArgs e)
{
string folderPath = string .Empty;

SaveFileDialo g savefiledialog1 = new SaveFileDialog();
savefiledialog1.Filter = Xml文件(* .xml)| * .xml |所有文件(*。 。*)| *。*;
savefiledialog1.RestoreDirectory = true ;

if (savefiledialog1.ShowDialog()== DialogResult.OK)
{
folderPath = savefiledialog1.FileName;


if (!System.IO.File.Exists(folderPath))
{
XmlDeclaration declaration = doc.CreateXmlDeclaration( 1.0 UTF-8 yes< /跨度>);
XmlComment comment = doc.CreateComment( 这是一个XML生成的文件);
XmlElement root = doc.CreateElement( calcs);
XmlElement calc = doc.CreateElement( calc);
XmlAttribute Name = doc.CreateAttribute( Name);
// 为每个节点添加值
Name.Value = textBox5。文本;


// 构建文档
doc .AppendChild(声明);
doc.AppendChild(评论);
doc.AppendChild(root);
root.AppendChild(calc);
calc.Attributes.Append(Name);

doc.Save(savefiledialog1.FileName);

}


// 显示确认消息
MessageBox.Show( 详细信息已添加到XML文件中。);

// 重置新输入的文本字段
// textBox5.Text = String.Empty;
}
}

private void openSignalChainToolStripMenuItem_Click( object sender,EventArgs e)
{
XmlDataDocument xmldoc = new XmlDataDocument();
OpenFileDialog openfiledialog1 = new OpenFileDialog();
// openfiledialog1.InitialDirectory =c:\\;
openfiledialog1.FileName = Document;
openfiledialog1.DefaultExt = 。xml;
openfiledialog1.Filter = xml file(.xml)| * .xml;
openfiledialog1.FilterIndex = 2 ;
Nullable< bool> result = Convert.ToBoolean(openfiledialog1.ShowDialog());
openfiledialog1.RestoreDirectory = true ;
if (result == true
{
string filename = openfiledialog1.FileName;
尝试
{
XmlNodeList xmlnode;
int i = 0 ;
string str = null ;
FileStream fs = new FileStream(filename,FileMode.Open,FileAccess.Read);
xmldoc.Load(fs);
xmlnode = xmldoc.GetElementsByTagName( Product);
for (i = 0 ; i < ; = xmlnode.Count - 1 ; i ++)
{
xmlnode [i] .ChildNodes.Item( 0 )InnerText.Trim();
str = xmlnode [i] .ChildNodes.Item( 0 )。InnerText.Trim()+ | + xmlnode [i] .ChildNodes.Item( 1 )。InnerText.Trim( )+ | + xmlnode [i] .ChildNodes.Item( 2 )InnerText.Trim();
MessageBox.Show(str);
textBox5.Text = str;
}
}
catch (Exception ex)
{
MessageBox.Show( 找不到文件!!!!);
}


}
}

解决方案

如果您使用DataSet作为您网格的数据,只需使用WriteXml保存数据,使用ReadXml将数据加载回网格。



http://msdn.microsoft.com/en-us/library/System.Data.DataSet %28V = vs.110%29.aspx

i have a windows form contains datagridview (having student information) and a menu(having items(save,open)
i want that

after filling all student information

--> if user click save button
save as window will open
-->then user entered the name of file and file will save (extension of file could be anything ..for example .sgc)
---> then all the data of datagridview that entered by user will save on that file
same as a want to open that file..

please help..!!!
Here is code..


private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
 {
     string folderPath = string.Empty;

     SaveFileDialog savefiledialog1 = new SaveFileDialog();
     savefiledialog1.Filter = "Xml files (*.xml)|*.xml|All files (*.*)|*.*";
     savefiledialog1.RestoreDirectory = true;

     if (savefiledialog1.ShowDialog() == DialogResult.OK)
     {
         folderPath = savefiledialog1.FileName;


                 if (!System.IO.File.Exists(folderPath))
                 {
                     XmlDeclaration declaration = doc.CreateXmlDeclaration("1.0", "UTF-8", "yes");
                     XmlComment comment = doc.CreateComment("This is an XML Generated File");
                     XmlElement root = doc.CreateElement("calcs");
                     XmlElement calc = doc.CreateElement("calc");
                     XmlAttribute Name = doc.CreateAttribute("Name");
                     //Add the values for each nodes
                     Name.Value = textBox5.Text;


                     //Construct the document
                     doc.AppendChild(declaration);
                     doc.AppendChild(comment);
                     doc.AppendChild(root);
                     root.AppendChild(calc);
                     calc.Attributes.Append(Name);

                     doc.Save(savefiledialog1.FileName);

                 }


                 //Show confirmation message
                 MessageBox.Show("Details have been added to the XML File.");

                 //Reset text fields for new input
                 //textBox5.Text = String.Empty;
             }
         }

 private void openSignalChainToolStripMenuItem_Click(object sender, EventArgs e)
 {
     XmlDataDocument xmldoc = new XmlDataDocument();
     OpenFileDialog openfiledialog1 = new OpenFileDialog();
   //  openfiledialog1.InitialDirectory = "c:\\";
     openfiledialog1.FileName = "Document";
     openfiledialog1.DefaultExt = ".xml";
     openfiledialog1.Filter = "xml file (.xml)|*.xml";
    openfiledialog1.FilterIndex = 2;
     Nullable<bool> result = Convert.ToBoolean(openfiledialog1.ShowDialog());
     openfiledialog1.RestoreDirectory = true;
     if (result==true)
     {
             string filename = openfiledialog1.FileName;
             try
             {
                 XmlNodeList xmlnode;
                 int i = 0;
                 string str = null;
                 FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read);
                 xmldoc.Load(fs);
                 xmlnode = xmldoc.GetElementsByTagName("Product");
                 for (i = 0; i <= xmlnode.Count - 1; i++)
                 {
                     xmlnode[i].ChildNodes.Item(0).InnerText.Trim();
                     str = xmlnode[i].ChildNodes.Item(0).InnerText.Trim() + " | " + xmlnode[i].ChildNodes.Item(1).InnerText.Trim() + " | " + xmlnode[i].ChildNodes.Item(2).InnerText.Trim();
                     MessageBox.Show(str);
                     textBox5.Text = str;
                 }
             }
             catch (Exception ex)
             {
                 MessageBox.Show("File can not found!!!!");
             }


     }
 }

解决方案

If you use DataSet as datasorce of yours grid just use WriteXml to save data and ReadXml to load data back to grid.

http://msdn.microsoft.com/en-us/library/System.Data.DataSet%28v=vs.110%29.aspx


这篇关于如何编写和读取xml文件的数据,以便在c#中保存windows窗体的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆