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

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.

1 Answer 1

1

Maybe try something like this:

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);
    }
Sign up to request clarification or add additional context in comments.

4 Comments

That would work out great! If only I hadn't made the huge mistake of putting XDocument instead of XmlDocument... Back to the drawing board for me.
If I use this method with my HTTP response it comes out null, if I use just the URL of the generated file then it displays 0 for some reason.
Problem has been resolved. Apparently there wasn't an error in my parsing, but an error in my HTTP request. Thanks for the help! The code above works PERFECTLY for getting the information needed.
I'm glad to hear it. Please don't forget to mark my response as the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.