码迷,mamicode.com
首页 > Windows程序 > 详细

winform生成XML文件

时间:2014-12-04 19:43:00      阅读:282      评论:0      收藏:0      [点我收藏+]

标签:winform   style   blog   http   io   ar   color   os   sp   

bubuko.com,布布扣
/// <summary>
        /// 生成XML
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCreate_Click(object sender, EventArgs e)
        {

            #region 验证、读值

            if (string.IsNullOrWhiteSpace(this.txtIP.Text))
            {
                MessageBox.Show("服务器IP不能为空");
                return;
            }
            if (string.IsNullOrWhiteSpace(this.txtPort.Text))
            {
                MessageBox.Show("服务器端口号不能为空");
                return;
            }
            if (string.IsNullOrWhiteSpace(this.txtUserName.Text))
            {
                MessageBox.Show("服务器用户不能为空");
                return;
            }
            if (string.IsNullOrWhiteSpace(this.txtPassword.Text))
            {
                MessageBox.Show("服务器密码不能为空");
                return;
            }
            string server = this.txtIP.Text;//服务
            string port = this.txtPort.Text;//端口
            string userName = this.txtUserName.Text;//用户
            string passWord = this.txtPassword.Text;//密码

            #endregion

            XmlDocument doc = new XmlDocument();
            XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
            doc.AppendChild(dec);
            //创建一个根节点(一级)
            XmlElement root = doc.CreateElement("FTPXML");
            doc.AppendChild(root);
            //创建节点(二级)
            XmlNode node = doc.CreateElement("FTPConfigXML");
            //创建节点(三级)
            //Server
            XmlElement elementServer = doc.CreateElement("Server");
            elementServer.SetAttribute("Name", "server");
            elementServer.SetAttribute("ID", "server");
            elementServer.InnerText = server;
            node.AppendChild(elementServer);
            //Port
            XmlElement elementPort = doc.CreateElement("Port");
            elementPort.SetAttribute("Name", "port");
            elementPort.SetAttribute("ID", "port");
            elementPort.InnerText = port;
            node.AppendChild(elementPort);
            //UserName
            XmlElement elementUserName = doc.CreateElement("UserName");
            elementUserName.SetAttribute("Name", "userName");
            elementUserName.SetAttribute("ID", "userName");
            elementUserName.InnerText = userName;
            node.AppendChild(elementUserName);
            //PassWord
            XmlElement elementPassWord = doc.CreateElement("PassWord");
            elementPassWord.SetAttribute("Name", "passWord");
            elementPassWord.SetAttribute("ID", "passWord");
            elementPassWord.InnerText = passWord;
            node.AppendChild(elementPassWord);
            //DateTime
            XmlElement elementDateTime = doc.CreateElement("DateTime");
            elementDateTime.SetAttribute("Name", "DateTime");
            elementDateTime.SetAttribute("ID", "DateTime");
            elementDateTime.InnerText = DateTime.Now.ToString();
            node.AppendChild(elementDateTime);

            root.AppendChild(node);
            string targetName = "FTPXML.xml";//保存文件名
            doc.Save(GetDemoUrl() + targetName);
            //Console.Write(doc.OuterXml);

        }

        /// <summary>
        /// 取消
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCancel_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
        /// <summary>
        /// 获取当前项目应用程序域
        /// </summary>
        /// <returns></returns>
        public static string GetDemoUrl()
        {
            string personImgPath = Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory) + Path.DirectorySeparatorChar.ToString();
            if (!Directory.Exists(personImgPath))
            {
                Directory.CreateDirectory(personImgPath);
            }
            return personImgPath;
        }
View Code

 

winform生成XML文件

标签:winform   style   blog   http   io   ar   color   os   sp   

原文地址:http://www.cnblogs.com/wuxiaohui/p/4143422.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!