zoukankan      html  css  js  c++  java
  • Xml文件读写

    #region Xml文件读写
            public void ReadXml()
            {
                //加载 
                XmlDocument doc = new XmlDocument();
                try
                {
                    doc.Load("Connection.xml");
                }
                catch
                {
                    doc.AppendChild(doc.CreateElement("Connection"));
                    XmlElement ComConfig = doc.CreateElement("Com");
                    ComConfig.InnerText = "Com4";
                    doc.DocumentElement.AppendChild(ComConfig);
                    XmlElement RateConfig = doc.CreateElement("Rate");
                    RateConfig.InnerText = "115200";
                    doc.DocumentElement.AppendChild(RateConfig);
                    XmlElement IsCheckedConfig = doc.CreateElement("IsChecked");
                    IsCheckedConfig.InnerText = "True";
                    doc.DocumentElement.AppendChild(IsCheckedConfig);
                    //保存 
                    XmlTextWriter xmlTextWriter = new XmlTextWriter("Connection.xml", Encoding.Default);
                    xmlTextWriter.Formatting = Formatting.Indented;
                    doc.Save(xmlTextWriter);
                    xmlTextWriter.Close();
                }
                //读取 
                cbCheck.Checked = doc.DocumentElement.SelectSingleNode("IsChecked").InnerText=="True"?true :false;
                if (cbCheck.Checked)
                {
                    cboCom.SelectedIndex = cboCom.FindStringExact(doc.DocumentElement.SelectSingleNode("Com").InnerText);
                    cboRate.SelectedIndex = cboRate.FindStringExact(doc.DocumentElement.SelectSingleNode("Rate").InnerText);  
                }
            }
            public void WriteXml()
            {
                //加载 
                XmlDocument doc = new XmlDocument();
                try
                {
                    doc.Load("Connection.xml");
                }
                catch
                {
                    doc.AppendChild(doc.CreateElement("Connection"));
                    XmlElement ComConfig = doc.CreateElement("Com");
                    ComConfig.InnerText = "Com4";
                    doc.DocumentElement.AppendChild(ComConfig);
                    XmlElement RateConfig = doc.CreateElement("Rate");
                    RateConfig.InnerText = "115200";
                    doc.DocumentElement.AppendChild(RateConfig);
                    XmlElement IsCheckedConfig = doc.CreateElement("IsChecked");
                    IsCheckedConfig.InnerText = "True";
                    doc.DocumentElement.AppendChild(IsCheckedConfig);
                    //保存 
                    XmlTextWriter xmlTextWriter = new XmlTextWriter("Connection.xml", Encoding.Default);
                    xmlTextWriter.Formatting = Formatting.Indented;
                    doc.Save(xmlTextWriter);
                    xmlTextWriter.Close();
                }
                //修改 
                doc.DocumentElement.SelectSingleNode("Com").InnerText = cboCom.Text.Trim();
                doc.DocumentElement.SelectSingleNode("Rate").InnerText = cboRate.Text.Trim();
                doc.DocumentElement.SelectSingleNode("IsChecked").InnerText = cbCheck.Checked.ToString();
                doc.Save("Connection.xml");
            }
            #endregion
  • 相关阅读:
    django缓存,信号,orm性能,多数据库配置
    git实战
    Web框架
    Windows Server 2012 R2 管理员密码忘记如何修改密码
    主机记录和记录值
    Python的类
    GenomicRangeQuery Find the minimal nucleotide from a range of sequence DNA.
    Countdiv-Codility Lesson5
    Windows域控时间不正确,设置互联网NTP服务器时间自动同步
    Missing Smallest positive integer
  • 原文地址:https://www.cnblogs.com/Iyce/p/2738815.html
Copyright © 2011-2022 走看看