XML Document SelectSingleNode returns null

前端 未结 5 960
你的背包
你的背包 2020-12-08 14:34

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         


        
5条回答
  •  轮回少年
    2020-12-08 15:19

    You can remove the namespace while reading the file, just disable the namespaces on the XmlTextReader:

    var request = (HttpWebRequest) WebRequest.Create(address);
    var response = (HttpWebResponse) request.GetResponse();
    var stream = response.GetResponseStream();
    
    if(stream != null)
    {
       var xmlReader = new XmlTextReader(stream);
       xmlReader.Namespaces = false;
       var xmlDocument = new XmlDocument();
       xmlDocument.Load(xmlReader);
       var node = xmlDocument.SelectSingleNode("RateQuote");
    }
    

    After that you don't have to care about the namespace while using XPath / LINQ on your XML-elements.

提交回复
热议问题