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();
            }
        }
  • 相关阅读:
    最新屏蔽微信举报方法
    C# WIN 生成机器码
    Quartz.net的快速简单上手使用以及防止IIS回收停止Job的处理
    MVC、Web API 请求接口报错“自定义错误模块不能识别此错误。”解决
    获取微信短链接的官方接口
    Window 通过cmd查看端口占用、相应进程、杀死进程
    微信域名检测、防封,微信跳转技术揭秘(二) -- 微信跳转揭秘
    微信域名检测、防封,微信跳转技术揭秘(一) -- 域名检测原理及防封方案
    各种比较方便给力的小工具
    《Git学习指南》学习笔记(三)
  • 原文地址:https://www.cnblogs.com/mingyan/p/1491293.html
Copyright © 2011-2022 走看看