• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
山石岩
天行健,君子以自强不息; 地势坤,君子以厚德载物!
博客园    首页    新随笔    联系   管理    订阅  订阅

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();
        }
    }
posted @ 2009-05-28 13:32  猥琐的疯子  阅读(527)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3