I am trying to read XML from stream reader and am also getting response XML. But when i try to read its nodes it is always returning null.
var request = (Htt
The problem is that you're asking for a RateQuote
element without a namespace - whereas the RateQuote
element is actually in the namespace with URI http://ratequote.usfnet.usfc.com/v2/x1
.
You can either use an XmlNamespaceManager
to address the namespace within your XPath, or use LINQ to XML which has very simple namespace handling:
var document = XDocument.Load(stream);
XNamespace ns = "http://ratequote.usfnet.usfc.com/v2/x1";
XElement rateQuote = document.Root.Element(ns + "RateQuote");
Personally I would use LINQ to XML if you possibly can - I find it much more pleasant to use than XmlDocument
. You can still use XPath if you want of course, but I personally prefer to use the querying methods.
EDIT: Note that the namespace defaulting applies to child elements too. So to find the TOTAL_COST
element you'd need:
XElement cost = document.Root
.Element(ns + "RateQuote")
.Element(ns + "TOTAL_COST");