以XML格式显示范围之外的节点的值

时间:2013-06-25 03:51:15

标签: c# xml xmlnode

我正在从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字段中。

有人可以引导我完成它吗?

3 个答案:

答案 0 :(得分:0)

不确定确切的情况,但在foreach循环之外声明User1和User2可能会有所帮助吗?这假设每个记录总会有一条记录。

答案 1 :(得分:0)

尝试如下,它会帮助你..

           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();
            }
        }

答案 2 :(得分:0)

我终于在读者范围内调用了该函数。它现在正在运作。

感谢大家的回答和指导。