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

 

posted on 2012-10-25 14:36  酒歌  阅读(221)  评论(0编辑  收藏  举报