zoukankan      html  css  js  c++  java
  • C#操作XML的代码

     
    绑定控件的使用方法          
      DataSet ds = new DataSet();
                ds.ReadXml(@"TuFaType.xml");
                for (int j = 0; j < ds.Tables[0].Rows.Count; j++)
                {
                    this.comboBoxType.Items.Add(ds.Tables[0].Rows[j]["type"]);

                }
                Hashtable ht = new Hashtable();
                for (int i = 0; i < comboBoxType.Items.Count; i++)
                {
                    if (!ht.ContainsValue(this.comboBoxType.Items.ToString()))
                    {
                        ht.Add(i, this.comboBoxType.Items.ToString());
                    }

                }
                this.comboBoxType.Items.Clear();
                foreach (DictionaryEntry de in ht)
                {
                    this.comboBoxType.Items.Add(de.Value);
                }
                comboBoxType.SelectedIndex = 0;
    使用XML记录事情与读取:
    private void Form1_Load(object sender, EventArgs e)
            {
                if (!File.Exists("user.xml"))
                {
                    
                }
                else
                {
                    readerXml();
                }
                
            }
            private void buttonSave_Click(object sender, EventArgs e)
            {
                writeXml(this.textBoxName.Text,this.textBoxPwd.Text);
            }

            private void readerXml()
            {
              
                XmlTextReader readerXml = new XmlTextReader("user.xml");
                XmlDocument doc = new XmlDocument();
                doc.Load("user.xml");
                XmlNode root = doc.DocumentElement;
                this.textBoxName.Text=root.SelectSingleNode("userName").InnerText;
                this.textBoxPwd.Text=root.SelectSingleNode("pwd").InnerText;

                
            }

            private void writeXml(string userName,string pwd)
            {
                CreateXml();

                XmlDocument doc = new XmlDocument();
                doc.Load("user.xml");
                XmlElement root = doc.DocumentElement;

                XmlElement userNames = doc.CreateElement("userName");
                userNames.InnerText = userName;
                XmlElement pwds = doc.CreateElement("pwd");
                pwds.InnerText = pwd;

                root.AppendChild(userNames);
                root.AppendChild(pwds);
                doc.Save("user.xml");
                  
            }

            private static void CreateXml()
            {
                XmlTextWriter writeXml = new XmlTextWriter("user.xml", Encoding.UTF8);
                writeXml.Formatting = Formatting.Indented;
                writeXml.Indentation = 5;
                writeXml.WriteStartDocument();

                writeXml.WriteStartElement("user");
                writeXml.Close();
            }
        }
  • 相关阅读:
    Win10 Theano Install Guide
    mysql 查看版本和是否支持分区
    [mysql]Date和String相互转换(DATE_FORMAT&STR_TO_DATE)
    mysql运用now(3)存储时间到毫秒
    mysql按天,按周,按月,按季度,按年统计数据
    对象的copy
    定时调度任务quartz
    mysql使用navicat编写调用存储过程
    mysql批量插入,批量更新
    Mybatis分页插件pagehelper的使用
  • 原文地址:https://www.cnblogs.com/mingyan/p/1491293.html
Copyright © 2011-2022 走看看