how to read the email address of the xml file selected node, Please help urgent RRS feed

  • Question

  • Hi

    I am writng the program to send the statement to customers writing the sql query using an xml file. Each customer part is separated by Customer attribute ID for example Customer1 and Customer2

    My sample XML

    <?xml version="1.0" encoding="utf-8" ?>
    <SQL ID="SQLS1">
         <!-- Select Rate From Product -->
    <SQL ID="SQLS2">
         <!-- select Qty from Product -->
    <MailBody>Please find statement</MailBody>
    <SQL ID="SQLS">
    <!-- Select Code from Customer -->
    <SQL ID="SQLS3">
    <!-- select name from supplier -->
    <SQL ID="SQLS4">
    <!-- select qty from purchase-->
    <MailBody>Please find statement</MailBody>

    Now I am trying to get all the email address  under the tag <EmailAddress>  in to the List<string>  variable EmailAddressList stored under each customer ID. So I am trying to set all email address and MailSubject into the corresponsing variable. My code is given below

    public class xmlResult { public string Tagid; public List<string> ListSql; public List<string> EmailAddressList; public string fileName; public string mailBody; } public static xmlResult GetSQL(XmlNode element) { var result = new List<String>(); var xmlData = new xmlResult(); var x = element.InnerText; xmlData.Tagid = element.Attributes["ID"].Value; var attributes = element.Attributes; foreach (var item in attributes) { string id = element.Attributes["ID"].Value; xmlData.ListSql= element .SelectNodes("SQL/comment()") .Cast<XmlComment>() .Select(c => c.Value.Trim()) .ToList(); xmlData.EmailAddress = How to get all the email address from the selectnode tag <EmailAddress> xmlData. fileName = ?? //get the filename under FilenameTag

    //So xmlAddress.EmailAddress[1] should be test1@gmail.com, xmlAddress.EmailAddress[2] should

    be test2@gmail.com . Then next xmlAddress.EmailAddress[1] should be test3@gmail.com,

    xmlAddress.EmailAddress[3] should be test4@gmail.com inside the foreach loop of attribute of

    CuCustomerID }

    Please help urgent


    • Edited by polachan Friday, July 5, 2019 8:50 AM
    Friday, July 5, 2019 8:45 AM

All replies

  • For instance, like this:

    XmlNodeList addresses = element.SelectNodes("EmailAddress"); // Or "Address"? your example is ambiguous: it ises Address for one customer and EmailAddress for the other
    foreach (var address in addresses)

    EDIT: Don't forget to initialise EmailAddressList as a new List<string> !!!
    Friday, July 5, 2019 9:38 AM
  • Is it possible  to get the email address the sameway gettting the sqlstatement inside the sqltag

                            .Select(c => c.Value.Trim())


    Friday, July 5, 2019 9:51 AM
  • Alberto 

    Your code is not working. One customer have more than one email address, the statement to be sent. When I apply your code the following error coming 

    XmlNodeList addresses = element.SelectNodes("EmailAddress");
                    foreach (var address in addresses)

    The error is 'the object doesnot contain a definition for innertext


    Friday, July 5, 2019 9:56 AM
  •                 result = 
                            .Select(c => c.InnerText.Trim())
    Friday, July 5, 2019 9:58 AM
  • The error is 'the object doesnot contain a definition for innertext

    Are you sure that this is the exact error message? This would indicate that you have written innertext all in lower case, instead of InnerText.

    Friday, July 5, 2019 10:01 AM
  • There's one address without content/innertext. so this may result in an exception. Since had various question about reading you xml, i prepared an 'all you can read' solution.

    I created a class customer:

        public class Customer
            public String Id { get; set; }
            public List<String>SQL { get; set; }
            public List<String>Address { get; set; }
            public String MailSubject { get; set; }
            public String MailBody { get; set; }
            public String Filename { get; set; }

    and lopp through the customers in your XML:

                var customerList = new List<Customer>();
                var doc = new XmlDocument();
                XmlNodeList subjectList = doc.GetElementsByTagName("CUSTOMER");
                var result = new List<String>();
                foreach (var child in subjectList)
                    if (child is XmlNode)
                        var element = (XmlNode) child;
                        customerList.Add(GetCustomerFromXml(element));                }

    and I read all data in one method (i used) LINQ in this case to get the Node's values:

    private Customer GetCustomerFromXml(XmlNode element) { var c = new Customer(); c.Id = element.Attributes["ID"].Value; c.SQL = new List<string>(); var sql = element.ChildNodes.Cast<XmlNode>().Where(i => i.Name == "SQL") .Select(i => i.FirstChild.InnerText); foreach (var s in sql) { c.SQL.Add(new Regex("[^ -~]+").Replace(s, "")); } c.Address = new List<string>(); var adr = element.ChildNodes.Cast<XmlNode>().Where(i => i.Name == "Address") .Select(i => i.FirstChild); foreach (var a in adr) { if (a == null) { continue; } c.Address.Add(a.InnerText); } c.MailSubject = element.ChildNodes.Cast<XmlNode>().Where(i => i.Name == "MailSubject") .Select(i => i.InnerText).FirstOrDefault(); c.MailBody = element.ChildNodes.Cast<XmlNode>().Where(i => i.Name == "MailBody") .Select(i => i.InnerText).FirstOrDefault(); c.Filename = element.ChildNodes.Cast<XmlNode>().Where(i => i.Name == "FILENAME") .Select(i => i.InnerText).FirstOrDefault(); return c; }

    Hope this helps

    Friday, July 5, 2019 10:14 AM
  • I have attached the error. Select (c=> not working


    Friday, July 5, 2019 10:16 AM
  •  foreach (var address in addresses)

    Innertext is not working


    Friday, July 5, 2019 10:17 AM
  • XmlNodeList represent a List of.... Object (unfortunately) and instances of Object don't necessarily have the InnerText Property. you have to do a cast to XmlNode like


    Alternate would be to adjust the selction of addresses:

    var addresses = element.SelectNodes("EmailAddress").Cast<XmlNode>();

    Friday, July 5, 2019 11:04 AM