2016-03-03 65 views
0

我有一个xml:提取使用实体从XML元素 - C#

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

我怎样才能从messageheaders各的MessageHeader汇总值? 我尝试了很多在这里找到的解决方案,但它不起作用。

XElement xml = XElement.Parse(response); 

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

此代码返回: 类型“System.NullReferenceException”的未处理的异常发生在X4SmsReceiver.dll

其他信息:对象没有设置为一个对象的一个​​实例。

谢谢你的帮助!

+0

其因为messageheaders有命名空间属性。试试这个参考http://stackoverflow.com/questions/5939509/xdocument-and-linq-returns-null-if-the-element-has-xmlns-attribute –

回答

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标记,然后通过反序列化您可以获取数据。