【问题标题】:LINQ XML select inside selectLINQ XML 选择里面的选择
【发布时间】:2011-12-17 09:50:25
【问题描述】:

我有以下 xml:

<transactionSearchResult>
<resultsInThisPage>14</resultsInThisPage>
<currentPage>1</currentPage>
<totalPages>1</totalPages>
<date>2011-10-28T11:20:10.000-02:00</date>
<transactions>
<transaction>
<date>2011-06-17T18:21:03.000-03:00</date>
<reference>ba3b7d36-a9e6-4d48-82b8-edf31a18fa49</reference>
<code>4E63AE64-24CF-4F70-A275-4B964E3DA6A5</code>
<type>1</type>
<status>4</status>
<paymentMethod>
<type>1</type>
</paymentMethod>
<grossAmount>1.00</grossAmount>
<discountAmount>0.00</discountAmount>
<feeAmount>0.46</feeAmount>
<netAmount>0.54</netAmount>
<extraAmount>0.00</extraAmount>
<lastEventDate>2011-07-01T19:27:36.000-03:00</lastEventDate>
</transaction>
<transaction>
  .
  .
  .
</transaction>
  .
  .
  .

linq 语句:

var transactions = 来自 xml.Descendants("transaction") 中的交易 选择新的 { code = transaction.Element("code").Value, 参考 = transaction.Element("reference").Value, date = transaction.Element("date").Value, type = transaction.Element("type"), 状态 = 交易。元素(“状态”), GrossAmount = transaction.Element("grossAmount").Value, discountAmount = transaction.Element("discountAmount").Value, feeAmount = transaction.Element("feeAmount").Value, netAmount = transaction.Element("netAmount").Value, extraAmount = transaction.Element("extraAmount").Value, lastEventDate = transaction.Element("lastEventDate").Value, paymentMethod = from p in transaction.Descendants("paymentMethod") 选择 p.Element("type").Value};

除了paymentMethod,一切正常...

【问题讨论】:

  • 您希望 paymentMethod 是什么样的?应该是什么类型的?

标签: c# xml linq select


【解决方案1】:

您是否正在寻找类似的东西?

paymentMethod = int.Parse(transaction.Element("paymentMethod").Element("type").Value)

【讨论】:

  • 另一种可能效果更好的语法:paymentMethod = (int)(transaction.Element("paymentMethod").Element("type"))
  • @JimWooley:感谢您的提示。我不知道你可以像那样隐式转换 XElements。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多