0
<response>
  <payment loanType="thirtyYearFixed">
    <rate>4.09</rate>
    <monthlyPrincipalAndInterest>410</monthlyPrincipalAndInterest>
    <monthlyMortgageInsurance>54</monthlyMortgageInsurance>
  </payment>
</response>

私の質問は、レート、monthlyPrincipalAndInterest、およびmonthlyMortgageInsurance から情報を取得するにはどうすればよいですか? 私はあらゆる方法を試しましたが、これを投稿する前の最後の手段として次のコードを使用して XDocument で停止しました。

Rate = root.SelectSingleNode("//response/payment[@loanType='thirtyYearFixed']/rate").InnerText;

これは rate 子要素のコードです。解析中の XML ファイルのこの部分の前にすべての情報を取得しましたが、これでレンガの壁にぶつかり、理解できないようです。//response/payment[@loanType='thirtyYearFixed'] 変数として XMLNodeList を使用し、次に nodeVar["rate"].InnerText を使用しても、null 参照エラーが発生しました。

これは私が見過ごした小さな断片になると感じていますが、オプションが不足しているだけでなく、時間が不足しています。

4

1 に答える 1

1

たぶん、次のようなことを試してください:

var xdoc = XDocument.Load(@"C:\Temp\doc.xml");
var node = xdoc.XPathSelectElements("./response/payment[@loanType='thirtyYearFixed']");
var query = from payment in  node             
            select new
            {
                rate                        = payment.XPathSelectElement("rate"),
                monthlyPrincipalAndInterest = payment.XPathSelectElement("monthlyPrincipalAndInterest"),
                monthlyMortgageInsurance    = payment.XPathSelectElement("monthlyMortgageInsurance")

            };

    foreach (var v in query)
    {
        Console.WriteLine(v.rate.Value);
        Console.WriteLine(v.monthlyPrincipalAndInterest.Value);
        Console.WriteLine(v.monthlyMortgageInsurance.Value);
    }
于 2013-09-29T03:31:32.473 に答える