【问题标题】:Extracting an element from XML using Entity - C#使用实体从 XML 中提取元素 - C#
【发布时间】:2016-03-03 18:53:45
【问题描述】:

我有一个 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();

此代码返回: X4SmsReceiver.dll 中出现“System.NullReferenceException”类型的未处理异常

附加信息:对象引用未设置为对象的实例。

感谢您的帮助!

【问题讨论】:

标签: xml entity


【解决方案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();

试试这个,效果很好

【讨论】:

  • 没问题。我想你也知道错误的原因。这是因为命名空间属性。 :)
【解决方案2】:

你可以使用XSD.exe设计一个xml标签类,然后通过反序列化就可以得到数据。

【讨论】:

    猜你喜欢
    • 2012-09-13
    • 1970-01-01
    • 1970-01-01
    • 2020-12-15
    • 1970-01-01
    • 2018-12-05
    • 1970-01-01
    • 2011-10-13
    • 1970-01-01
    相关资源
    最近更新 更多