I have referred XPathSelectElement select the second when there is more than one and 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;
}