0

I have a xml:

<messageheaders startindex="0" count="3" totalcount="3" xmlns="http://api.esendex.com/ns/">
- <messageheader id="74dfea3e-a196-4ae0-9595-105c9f59cc01" uri="https://api.esendex.com/v1.0/messageheaders/74dfea3e-a196-4ae0-9595-105c9f59cc01">
  <reference>EX0196276</reference> 
  <status>Submitted</status> 
  <sentat>2016-03-03T08:01:32.97Z</sentat> 
  <laststatusat>2016-03-03T08:01:32.97Z</laststatusat> 
  <submittedat>2016-03-03T08:01:32.97Z</submittedat> 
  <receivedat>2016-03-03T08:01:32.97Z</receivedat> 
  <type>SMS</type> 
- <to>
  <phonenumber>34627176544</phonenumber> 
  </to>
- <from>
  <phonenumber>447908583018</phonenumber> 
  </from>
  <summary>837680710A4714672983100303090100640000000000000...</summary> 
  <body id="74dfea3e-a196-4ae0-9595-105c9f59cc01" uri="https://api.esendex.com/v1.0/messageheaders/74dfea3e-a196-4ae0-9595-105c9f59cc01/body" /> 
  <direction>Inbound</direction> 
  <parts>1</parts> 
  <readat>0001-01-01T00:00:00Z</readat> 
  </messageheader>
- <messageheader id="8e835eda-d6d7-46e4-ab44-4df20bd133a4" uri="https://api.esendex.com/v1.0/messageheaders/8e835eda-d6d7-46e4-ab44-4df20bd133a4">
  <reference>EX0196276</reference> 
  <status>Submitted</status> 
  <sentat>2016-03-03T07:00:30.957Z</sentat> 
  <laststatusat>2016-03-03T07:00:30.957Z</laststatusat> 
  <submittedat>2016-03-03T07:00:30.957Z</submittedat> 
  <receivedat>2016-03-03T07:00:30.957Z</receivedat> 
  <type>SMS</type> 
- <to>
  <phonenumber>34627176544</phonenumber> 
  </to>
- <from>
  <phonenumber>447944489977</phonenumber> 
  </from>
  <summary>B3EB80710A470F42A782100303080000640000000000000...</summary> 
  <body id="8e835eda-d6d7-46e4-ab44-4df20bd133a4" uri="https://api.esendex.com/v1.0/messageheaders/8e835eda-d6d7-46e4-ab44-4df20bd133a4/body" /> 
  <direction>Inbound</direction> 
  <parts>1</parts> 
  <readat>2016-03-03T07:08:19.963Z</readat> 
  <readby>[email protected]</readby> 
  </messageheader>
- <messageheader id="f9fd0867-7659-4042-a1d6-edd8699b19b3" uri="https://api.esendex.com/v1.0/messageheaders/f9fd0867-7659-4042-a1d6-edd8699b19b3">
  <reference>EX0196276</reference> 
  <status>Submitted</status> 
  <sentat>2016-03-03T06:01:33.007Z</sentat> 
  <laststatusat>2016-03-03T06:01:33.007Z</laststatusat> 
  <submittedat>2016-03-03T06:01:33.007Z</submittedat> 
  <receivedat>2016-03-03T06:01:33.007Z</receivedat> 
  <type>SMS</type> 
- <to>
  <phonenumber>34627176544</phonenumber> 
  </to>
- <from>
  <phonenumber>447944489977</phonenumber> 
  </from>
  <summary>B3EA80710A470F42A780100303070100640000000000000...</summary> 
  <body id="f9fd0867-7659-4042-a1d6-edd8699b19b3" uri="https://api.esendex.com/v1.0/messageheaders/f9fd0867-7659-4042-a1d6-edd8699b19b3/body" /> 
  <direction>Inbound</direction> 
  <parts>1</parts> 
  <readat>2016-03-03T07:08:05.497Z</readat> 
  <readby>[email protected]</readby> 
  </messageheader>
  </messageheaders>

How can I get the summary value from each messageheader in messageheaders? I tried many solutions found here but it doesnt work.

XElement xml = XElement.Parse(response);

var result = xml
.Elements()
.Select(s => new
{
Summary = s.Element("summary").Value.ToString()
}).ToList();

This code returns: An unhandled exception of type 'System.NullReferenceException' occurred in X4SmsReceiver.dll

Additional information: Object reference not set to an instance of an object.

Thank you for all your help!

1

2 Answers 2

1
XNamespace xns = "http://api.esendex.com/ns/";
        var data = xdoc.Element(xns + "messageheaders").Elements(xns+"messageheader").Select(x => new { Summary = x.Element(xns+"summary")}).ToList();

Try this it works

Sign up to request clarification or add additional context in comments.

1 Comment

No Problem. I think you also got to know the reason for the error. Its because of the namespace attribute. :)
0

You can use XSD.exe to design a class of xml tags and then by deserializing you can get the data.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.