none
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" ?>
    <STATEMENT>
    <CUSTOMER ID = "CUSTOMER1">
    <SQL ID="SQLS1">
         <!-- Select Rate From Product -->
     </SQL>
    <SQL ID="SQLS2">
         <!-- select Qty from Product -->
     </SQL>
    <Address>test1@gmail.com</Address>
    <Address>test2@gmail.com</Address>
    <Address></Address>
    <MailSubject>Statement</MailSubject>
    <MailBody>Please find statement</MailBody>
    <FILENAME>Statement2</FILENAME>
    </CUSTOMER>
    <CUSTOMER ID = "CUSTOMER2">
    <SQL ID="SQLS">
    <!-- Select Code from Customer -->
     </SQL>
    <SQL ID="SQLS3">
    <!-- select name from supplier -->
     </SQL>
    <SQL ID="SQLS4">
    <!-- select qty from purchase-->
     </SQL>
    
    <EmailAddress>test3@gmail.com</Address>
    <EmailAddress>test4@gmail.com</Address>
    <MailSubject>statement</MailSubject>
    <MailBody>Please find statement</MailBody>
    <FILENAME>Statement2</FILENAME>
    </CUSTOMER>
    </STATEMENT>

    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


    polachan



    • 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)
    {
        EmailAddressList.Add(address.InnerText);
    }


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

    xmlData.ListSql= 
                            element
                            .SelectNodes("SQL/comment()")
                            .Cast<XmlComment>()
                            .Select(c => c.Value.Trim())
                            .ToList();


    polachan

    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)
                    {
                        xmlData.EmailAddressList.Add(address.InnerText);
                    }

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


    polachan

    Friday, July 5, 2019 9:56 AM
  •                 result = 
                            element
                            .SelectNodes("EmailAddress")
                            .Select(c => c.InnerText.Trim())
                            .ToList();
    Friday, July 5, 2019 9:58 AM
    Moderator
  • 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
    Moderator
  • 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();
                doc.Load("XMLFile1.xml");
                
                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


    polachan

    Friday, July 5, 2019 10:16 AM
  •  foreach (var address in addresses)
                    {
                        xmlData.EmailAddressList.Add(address.InnerText);
                    }

    Innertext is not working


    polachan

    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

    ((XmlNode)item).InnerText

    Alternate would be to adjust the selction of addresses:

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

    Friday, July 5, 2019 11:04 AM