<DIV id=blogDetailDiv style="FONT-SIZE: 12px">xml让数据操作更自由!<BR>一个简单的xml文件的读写代码<BR>//写文件<BR>SaveFileDialog sfdlg = new SaveFileDialog();<BR> if (sfdlg.ShowDialog() != DialogResult.OK)<BR> return;<BR> XmlTextWriter xtw = new XmlTextWriter(sfdlg.FileName, Encoding.UTF8);//构建xml文件流<BR> xtw.WriteStartDocument();//开始写文档<BR> xtw.WriteComment("一个xml文件写的示例");//添加注释<BR> xtw.WriteStartElement("Login");//开始写元素<BR> xtw.WriteElementString("Title", textBox1.Text);<BR> xtw.WriteElementString("Name", textBox2.Text);<BR> xtw.WriteElementString("Content", richTextBox1.Text);<BR> xtw.WriteEndElement();//结束写元素<BR> xtw.WriteEndDocument();//结束写文档<BR> xtw.Close(); <BR><BR>//读文件<BR>OpenFileDialog ofdlg = new OpenFileDialog();<BR> ofdlg.Filter = "XML文件|*.xml";<BR> if( ofdlg.ShowDialog() == DialogResult.OK)<BR> {<BR> LoadXmlFile(ofdlg.FileName);<BR> FillData();<BR> }<BR>List<MyLogin> listLogin = new List<MyLogin>();<BR> void LoadXmlFile(string path)<BR> {<BR> MyLogin login = new MyLogin();<BR> XmlDocument xmldoc = new XmlDocument();<BR> xmldoc.Load(path);<BR> XmlNode node = xmldoc.DocumentElement;<BR> foreach (XmlNode xn in node.ChildNodes)<BR> {<BR> switch (xn.Name)<BR> {<BR> case "Title":<BR> login.Title = xn.InnerText; break;<BR> case "Name":<BR> login.Name = xn.InnerText; break;<BR> case "Content":<BR> login.Content = xn.InnerText; break;<BR> }<BR> }<BR> listLogin.Add(login);<BR> }<BR> int index = 0;<BR> void FillData()<BR> {<BR> MyLogin ml = listLogin[index];<BR> textBox1.Text = ml.Title;<BR> textBox2.Text = ml.Name;<BR> richTextBox1.Text = ml.Content;<BR> } </DIV>
<DIV style="FONT-SIZE: 12px"> </DIV>
<DIV style="FONT-SIZE: 12px"><A href="http://www.bdqn-sx.com">www.bdqn-sx.com</A></DIV> |
|