none
How can I store an xml node value into a string variable RRS feed

  • Question

  • I am trying to store he following value of xml into a string variable. It is not working. But if I am using List<string> it would be working. But I don't need to use list string variable  for mailsubject. The mailsubject is only one tag. there is no mutiple tag. So I would like to use string variable to store mailsubject rather than List<string> Please help

    string _mailsubject =
                             element
                            .SelectNodes("MailSubject")
                            .Cast<XmlNode>()
                            .Select(c => c.InnerText.Trim())
                            .ToString(); //  it is not working the value is not coming from xml
    <Address>test1@gmail.com</Address>
    <Address>test2@gmail.com</Address>
    <Address></Address>
    <MailSubject>Statement</MailSubject>
    <MailBody>Please find statement</MailBody>
    <FILENAME>Statement2</FILENAME>


    polachan

    Friday, July 5, 2019 11:05 AM

Answers

  • SelectNodes() will always give you a List of items even if only one matching item was found. In that case you will get a List with one entry.

    Replace .ToString() with .FirstOrDefault() and you're done.

    Edit:

    element.SelectNodes("MailSubject").Item(0).InnerText

    will do the same. But be sure you have at least one item (index 0) found.

    Friday, July 5, 2019 11:13 AM