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
  • 相关阅读:
    centOS 6 服务管理与服务脚本
    centOS 6启动流程
    shell脚本之流程控制
    centOS7网络配置(nmcli,bonding,网络组)
    模拟主机跨路由通信实验
    网络配置之基本网络配置(cenos6)
    网络基础之IP地址与子网划分
    网络基础之网络层
    我的BO之数据保护
    我的BO之强类型
  • 原文地址:https://www.cnblogs.com/Iyce/p/2738815.html
Copyright © 2011-2022 走看看