我有一个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>andrej.dolar@iskra.eu</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>andrej.dolar@iskra.eu</readby>
</messageheader>
</messageheaders>
如何从messageheaders中的每个messageheader获取汇总值? 我在这里尝试了很多解决方案,但它不起作用。
XElement xml = XElement.Parse(response);
var result = xml
.Elements()
.Select(s => new
{
Summary = s.Element("summary").Value.ToString()
}).ToList();
此代码返回: 类型&#39; System.NullReferenceException&#39;的未处理异常发生在X4SmsReceiver.dll
中附加信息:未将对象引用设置为对象的实例。
感谢您的帮助!
答案 0 :(得分: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();
试试这个有效
答案 1 :(得分:0)
您可以使用XSD.exe设计一类xml标记,然后通过反序列化可以获取数据。