2016-03-03 70 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標記,然後通過反序列化您可以獲取數據。