Saturday, 28 September 2013

How to get XML information from a child in an Element with a attribute using XDocument in C#?

How to get XML information from a child in an Element with a attribute
using XDocument in C#?

<response>
<payment loanType="thirtyYearFixed">
<rate>4.09</rate>
<monthlyPrincipalAndInterest>410</monthlyPrincipalAndInterest>
<monthlyMortgageInsurance>54</monthlyMortgageInsurance>
</payment>
My question is how do I get the information from rate,
monthlyPrincipalAndInterest, and monthlyMortgageInsurance? I have tried
every different way and have stopped with XDocument using the following
code as my last resort prior to posting this:
Rate =
root.SelectSingleNode("//response/payment[@loanType='thirtyYearFixed']/rate").InnerText;
This is just the code for the rate child element. I have get all
information prior to this portion in the XML file I am parsing but I hit a
brick wall with this and can't seem to figure it out. I've even used
XMLNodeList with the base //response/payment[@loanType='thirtyYearFixed']
as the variable then nodeVar["rate"].InnerText and still got a null
reference error.
I have a feeling this is going to be some small piece I over looked but
I'm not only running out of options I'm running out of time. Thanks for
any help!

No comments:

Post a Comment