c#创建xml读取xml方法并判断文件目录以及文件是否存在

        public void cxml()
        {
            try
            {
                string saveFileName = "GeRen.xml";
                XmlDocument xmlDoc = new XmlDocument(); // 创建dom对象
                string sXml = "";
                sXml += "";
                    sXml += "1";
                    sXml += "3";
                    sXml += "4";
                    sXml += "4";
                    sXml += "4";
                sXml += "
";
                xmlDoc.LoadXml(sXml);
                if (!Directory.Exists("config"))//若文件夹不存在则新建文件夹  
                {
                    Directory.CreateDirectory("config"); //新建文件86夹  
                }
                xmlDoc.Save("config//" + saveFileName); // 保存文件
                label3.Text="成功生成[" + saveFileName + "]!";
            }
            catch (Exception)
            {
                label3.Text="生成XML文件失败!";
            }
        }


        public StringBuilder rxml()
        {
            StringBuilder dbstring = new StringBuilder();
            if(!File.Exists("config//GeRen.xml")){
                return dbstring;
            }
            XmlReader red = XmlReader.Create("config//GeRen.xml");
           
            if (red.ReadToFollowing("Root"))
            {
                if (red.ReadToFollowing("ip")) dbstring.Append(red.ReadString());
                if(red.ReadToFollowing("username")) dbstring.Append(red.ReadString());
                if (red.ReadToFollowing("password")) dbstring.Append(red.ReadString());
                if (red.ReadToFollowing("duankou")) dbstring.Append(red.ReadString());
                if (red.ReadToFollowing("dbname")) dbstring.Append(red.ReadString());
            }
            return dbstring;
        }

你可能感兴趣的:(c#,xml)