[XML]读取Config配置文件值

本文介绍了一个基于配置文件的邮件发送程序,通过修改配置文件而非代码即可调整邮件参数,包括收件人、主题、正文等。文章提供了使用C#实现的具体代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

通常我们经常配置一些信息在配置文件和XML中,在不修改程序的前提下修改配置文件或者XML就可以实现。

案列通过邮件中转人发送邮件:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Mail;

namespace DzOpenPlat.Core
{
    /// <summary>
    /// MailSend 的摘要说明
    /// 作者:龚德辉
    /// 日期:2012-08-03
    /// </summary>
    public class MailSend
    {
        public MailSend()
        {
        }
        private string mailto;
        private string mailsubject;
        private string mailbody;
        private string attech;
        public string Mailto { set { mailto = value; } }
        public string Mailsubject { set { mailsubject = value; } }
        public string Mailbody { set { mailbody = value; } }
        public string Attech { set { attech = value; } }
       
        /// <summary>
        /// 创建Mail,填入发送人,主题,內容
        /// </summary>
        /// <returns></returns>
        public string Send()
        {
            try
            {   
                //处理多个收件人
                MailMessage mlMsg = new MailMessage();
                string[] toArray = mailto.Split(',');
                foreach (string i in toArray)
                {
                    mlMsg.To.Add(new MailAddress(i));
                }
                mlMsg.Subject = mailsubject;
                mlMsg.Body = mailbody;
                mlMsg.IsBodyHtml = true;
                BaseConfig bc = new BaseConfig("~/Config/SmtpSetting.config");
                mlMsg.From = new MailAddress(bc.GetConfigValue("UserName"), bc.GetConfigValue("AuthorName"));
                SmtpClient smtpClient = new SmtpClient(bc.GetConfigValue("SmtpServer"));
                smtpClient.Credentials = new NetworkCredential(bc.GetConfigValue("UserName"), bc.GetConfigValue("Pwd"));
                smtpClient.Timeout = 4000;
                smtpClient.EnableSsl = false;
                smtpClient.Send(mlMsg);
                return "发送成功";
            }
            catch (Exception e)
            {
                return e.ToString();
            }
        }

    }
}

BaseConfig:

上下左右

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Web;
namespace DzOpenPlat.Core
{
    public class BaseConfig
    {
        public string  configPath= string.Empty;
        public BaseConfig(string configPath) 
        {
            this.configPath = configPath;
        }
        /// <summary>
        /// 得到配置文件
        /// </summary>
        /// <param name="Item"></param>
        /// <returns></returns>
        public  string getConfigParamvalue(string Item)
        {
            return string.Empty;
        }
        /// <summary>
        /// 读xxx.config取配置文件
        /// </summary>
        /// <param name="Target"></param>
        /// <returns></returns>
        public  string GetConfigValue(string Target)
        {
            string path = HttpContext.Current.Server.MapPath(configPath);
            return GetConfigValue(Target, path);
        }
        /// <summary>
        /// 读xxx.config取配置文件
        /// </summary>
        /// <param name="Target"></param>
        /// <param name="ConfigPathName"></param>
        /// <returns></returns>
        internal string GetConfigValue(string Target, string XmlPath)
        {
            System.Xml.XmlDocument xdoc = new XmlDocument();
            xdoc.Load(XmlPath);
            XmlElement root = xdoc.DocumentElement;
            XmlNodeList elemList = root.GetElementsByTagName(Target);
            try
            {
                return elemList[0].InnerText;
            }
            catch
            {
                return null;
            }
        }

        internal string GetConfigValue(string Target, string XmlPath, string strattrbute)
        {
            System.Xml.XmlDocument xdoc = new XmlDocument();
            xdoc.Load(XmlPath);
            XmlElement root = xdoc.DocumentElement;
            XmlNodeList elemList = root.GetElementsByTagName(Target);
            try
            {
                return elemList[0].Attributes[strattrbute].ToString();
            }
            catch
            {
                return null;
            }
        }

        /// <summary>
        /// 返回该节点下的此属性的值。
        /// </summary>
        /// <param name="Target"></param>
        /// <param name="strattrbute"></param>
        /// <returns></returns>
        public  string GetConfigAttrbute(string Target, string strattrbute)
        {
            string path = HttpContext.Current.Server.MapPath(configPath);
            return GetConfigValue(Target, path, strattrbute);
        }
    }

}



 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

厦门德仔

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值