在XML中显示作用域之外的节点的值

本文关键字:节点 作用域 XML 显示 | 更新日期: 2023-09-27 18:19:42

我正在从XML文件中读取节点值,并希望在XML读取器范围之外显示数据。但每当我尝试时,它都会给我一个错误。

try
    {
        string filename = Server.MapPath("~") + "/Blah" + "/Blah.xml";
        XmlDocument doc = new XmlDocument();
        doc.Load(filename);
        XmlNodeList Email = doc.GetElementsByTagName("EmailTo");
        foreach (XmlNode node in Email)
        {
            XmlElement MailElement = (XmlElement)node;
            string User1= CDBInteract.formatSQLInput(MailElement.GetElementsByTagName("User1")[0].InnerText);
            string User2= CDBInteract.formatSQLInput(MailElement.GetElementsByTagName("User2")[0].InnerText);
        }
        #region Mail Sending 
        dtrecord = (DataTable)ViewState["datatable"];
        Utils util = new Utils();
        string Body = "";
        if (dtrecord.Rows.Count > 0)
        {
            DataView dv = dtrecord.DefaultView;
            dv.Sort = "FamNmae";
            DataTable Dts = dv.ToTable();

            string SUName = CDBInteract.formatInput(ReadConfigSettings.Get("SUName"));
            string SUEmail= CDBInteract.formatInput(ReadConfigSettings.Get("SUEmail"));
            string SmtpClient = CDBInteract.formatInput(ReadConfigSettings.Get("SmtpClient"));
            string EmailTo = CDBInteract.formatInput(ReadConfigSettings.Get("EmailTo"));
            string BCC= CDBInteract.formatInput(ReadConfigSettings.Get("EmailTo"));
            int m = util.SendMail(SUName , SUEmail, EmailTo, BCC, SmtpClient, "Blah", Body);
        }

在这里,我希望User1和User2的值显示在EmailTo和BCC字段中。

有人能引导我通过吗?

在XML中显示作用域之外的节点的值

不确定确切的场景,但在foreach循环之外声明User1和User2可能会有所帮助?这是假设他们每个人总是有一个记录。

试试下面的方法,它会对你有所帮助。。

           try
            {
                string filename = Server.MapPath("~") + "/Blah" + "/Blah.xml";
                XmlDocument doc = new XmlDocument();
                doc.Load(filename);    
                XmlNodeList Email = doc.GetElementsByTagName("EmailTo");
                dtrecord = (DataTable)ViewState["datatable"];
                foreach (XmlNode node in Email)
                {
                    XmlElement MailElement = (XmlElement)node;    
                    string User1 = CDBInteract.formatSQLInput(MailElement.GetElementsByTagName("User1")[0].InnerText);
                    string User2 = CDBInteract.formatSQLInput(MailElement.GetElementsByTagName("User2")[0].InnerText);
                    Sendemail(dtrecord, User1, User2);
                }               
            }
            catch (Exception ex)
            {
            }

       //Create a separate method for sending email
       private void Sendemail(DataTable dtrecord, string emailto, string bcc)
        {
            try
            {
                Utils util = new Utils();
                string Body = "";
                if (dtrecord.Rows.Count > 0)
                {
                    DataView dv = dtrecord.DefaultView;
                    dv.Sort = "FamNmae";
                    DataTable Dts = dv.ToTable();
                    string SUName = CDBInteract.formatInput(ReadConfigSettings.Get("SUName"));
                    string SUEmail = CDBInteract.formatInput(ReadConfigSettings.Get("SUEmail"));
                    string SmtpClient = CDBInteract.formatInput(ReadConfigSettings.Get("SmtpClient"));
                    string EmailTo = emailto;
                    string BCC = bcc;
                    int m = util.SendMail(SUName, SUEmail, EmailTo, BCC, SmtpClient, "Blah", Body);
                }
            }
            catch (Exception ex)
            {
                throw new Exception();
            }
        }

我最终调用了读取器范围内的函数。现在它正在发挥作用。

感谢大家的回答和指导。