Question: I have referred [XPathSelectElement select the second when there is more than one](https://stackoverflow.com/questions/13581556/xpathselectelement-select-the-second-when-there-is-more-than-one) and [XPath and XPathSelectElement](https://stackoverflow.com/questions/6289784/xpath-and-xpathselectelement). But this is a different question.

I have following xml. I need to find out the Message element value (from StatusDetail) corresponding to Sequence 2. If there is no sequence 2 present, it should return null. What is the best way to do this in C# using `XPathSelectElement`?

Note: There can be any number of StatusDetail (or not at all)

Note: The StatusDetail element can be in any order. We need to look only for the value “2” in 2

**CODE**

```
 XDocument xDoc = XDocument.Parse(@"  
          <Status>
            <StatusMsg>
                <StatusType>INVOICE</StatusType>
                <StatusCode>READYPAY</StatusCode>
                <StatusTimestamp>2013-03-19T21:20:54Z</StatusTimestamp>
                <StatusDetail>
                <Sequence>1</Sequence>
                <Message>.Document posted successfully </Message>
                </StatusDetail>
                <StatusDetail>
                <Sequence>2 </Sequence>
                <Message>Invoice is ready for pay</Message>
                </StatusDetail>
            </StatusMsg>
            </Status>
           ");

 var statusDetails = xDoc.XPathSelectElements(@"Status/StatusMsg/StatusDetail");

```

**UPDATE**

Following is the solution that I am using based on the selected answer

```
var statusDetails = xDoc.XPathSelectElements(@"Status/StatusMsg/StatusDetail/Sequence[text()=2]/../Message").FirstOrDefault();
if (statusDetails != null)
{
     selectedMessage = statusDetails.Value;
}

```

Answer: 
You can also convert the user input (string) to lowercase/Uppercase and then use in it in `if` . This will make it possible to use the code even if the user enter "JaN" "jAn" or "JAN"
You can use the atoi function to convert string into int

example:
a = "25";
b = atoi(a);

<http://www.cplusplus.com/reference/clibrary/cstdlib/atoi/>