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: 
try this way.. 

```
void displayType1(int day, string month, int year)
{
   cout << month <<" "<< day;
   if(day == 1) cout << "st, ";
   if(day == 2) cout << "nd, ";
   if(day == 3) cout << "rd, ";
   if(day > 3) cout << "th, ";
   cout << year << "\n"; 
}

void displayType2(int day, string month, int year)
{
   map<string, int> m;
   m["January"] = 1;
   m["February"] = 2;
   m["March"] = 3;
   m["April"] = 4;
   m["May"] = 5;
   m["June"] = 6;
   m["July"] = 7;
   m["August"] = 8;
   m["September"] = 9;
   m["October"] = 10;
   m["November"] = 11;
   m["December"] = 12;

   cout << m[month] << "-" << day << "-" << year << "\n";
}

.. and so on

void ConvertAndDisplayDate(string date, short type)
{
   ostringstream os(date);
   int day, year;
   string month;
   os >> day >> month >> year;

   switch(type) {
     case 1:
       displayType1(day, month, year);
       break;
    case 2:
       displayType2(day, month, year);
       break;
    case 3:
       displayType3(day, month, year);
       break;
    case 4:
       displayType4(day, month, year);
        break;
   }
}

```
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/>