LINQ XML选择里面选择

时间:2011-10-28 15:41:18

标签: c# xml linq select

我有以下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 = from transaction in xml.Descendants("transaction")
           select new
           {
                code = transaction.Element("code").Value,
                reference = transaction.Element("reference").Value,
                date = transaction.Element("date").Value,
                type = transaction.Element("type"),
                status = transaction.Element("status"),
                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")
                                     select  p.Element("type").Value}; 

除了付款方式,一切正常......

1 个答案:

答案 0 :(得分:1)

你在寻找更像这样的东西吗?

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