XML linq need detail info on exception



I am using xml linq on my project. I am dealing with very large xml's for easy understanding purpose I have mentioned small sample xml.



<soap:Envelope xmlns:soap="http://ift.tt/sVJIaE">
<soap:Body>
<StackOverflowReply xmlns="http://ift.tt/1tLkBXu">
<processStatus>
<statusCode1>P</statusCode1>
<statusCode2>P</statusCode2>
<statusCode3>P</statusCode3>
<statusCode4>P</statusCode4>
</processStatus>
</StackOverflowReply>
</soap:Body>


Following is C# xml linq



XNamespace x = "http://ift.tt/1tLkBXu";
var result = from StackOverflowReply in XDocument.Parse(Myxml).Descendants(x + "Security_AuthenticateReply")
select new
{
status1 = StackOverflowReply.Element(x + "processStatus").Element(x + "statusCode1").Value,
status2 = StackOverflowReply.Element(x + "processStatus").Element(x + "statusCode2").Value,
status3 = StackOverflowReply.Element(x + "processStatus").Element(x + "statusCode3").Value,
status4 = StackOverflowReply.Element(x + "processStatus").Element(x + "statusCode4").Value,
status5 = StackOverflowReply.Element(x + "processStatus").Element(x + "statusCode5").Value,
};


Here I am getting exception like "Object reference not set to an instance of an object.". Because the tag



<statusCode5>


was not in my xml.In this case I want to get detail exception message like "Missing tag statusCode5". Please guide me how to get this message from my exception.


No comments:

Post a Comment