c# - Retrieve data from xml file -


below sample xml file data i'm after

<ns2:response/"> <cachekey>-7ca7484a:13e22cb1399:-47c0</cachekey> <cachelocation>10.186.168.39:7302</cachelocation> </ns2:response> 

i have 2 classes, 1 class getting xml data, class passes data class have many other classes extract data.

public static string getxmldata() {     string xmlpath = "url";     var wc = new webclient();     wc.headers.add(httprequestheader.contenttype, "application/xml");     wc.headers.add(httprequestheader.accept, "application/xml");     var xmldata = wc.downloadstring(new uri(xmlpath));      var xmldoc = xdocument.parse(xmldata);      return xmldoc.tostring(); } 

the second clas has following

public class getcachekey {     public string cachekey { get; set; } } 

below i'm having problem:

public ienumerable<getcachekey> cachekey() {     var doc = xdocument.parse(getxmldata());     var xkey = c in doc.descendants("response")                 select new getcachekey()                     {                         cachekey = (string)doc.element("cachekey").value                       };      return xkey; } 

in debugger, returns empty = "enumeration yielded no results"

so how can value of cachekey?

there's 2 problems code. first of all, doc.descendants("response") not yield results, because you've omitted ns2 bit. secondly, doc.element("cachekey").value should c.element("cachekey").value:

xnamespace ns2 = "http://namespaceurihere";  var xkey = c in doc.descendants(ns2 + "response")            select new getcachekey()            {                cachekey = c.element("cachekey").value            }; 

not issue, string cast in code redundant.

the namespace uri should whatever uri removed xml when edited posting. confuses people however, because <ns2:response/"> not valid xml.


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -