c# - Why can I not find the XElements in my XML document? -


i know repeated question on so, answers haven't helped out. trying details of book using classify api c#. gives xml output like:

<classify xmlns="http://classify.oclc.org">     <response code="2"/>     <!--         classify product of oclc online computer library center: http://classify.oclc.org     -->     <work author="coelho, paulo | clarke, alan [author; translator] | sampere, daniel, 1985- [illustrator; draftsman] | lemmens, harrie, 1953- [translator] | smith, james noel | ruiz, derek, 1980- [adapter]" editions="176" format="book" holdings="7530" itemtype="itemtype-book" pswid="1151562357" title="the alchemist">123115868</work>     <authors>         <author lc="n94113544" viaf="65709090">clarke, alan [author; translator]</author>         <author lc="no2005013941" viaf="12032914">lemmens, harrie, 1953- [translator]</author>         <author lc="no2010190009" viaf="160023476">sampere, daniel, 1985- [illustrator; draftsman]</author>         <author lc="n86099875" viaf="52700">coelho, paulo</author>     </authors>     <orderby>hold desc</orderby>     <input type="stdnbr">9780754073963</input>     <!-- snip --> </classify> 

i have worked xml in php, not c# new , tutorials regarding everywhere tell different things making me confused , how it. want these things xml:

  • title of book ( in work element )
  • authors of book ( in work element )
  • isbn of book ( in input element - type stdnbr )
  • most popular ddc ( in recommendations > ddc > mostpopular - attribute [nsfa] )

but examples have seen on here , on other forums nullreferenceexception.

code:

ps: it's not homework.

public frmadditem(string xml) {     initializecomponent();     string itemname = null, itemauth = null, itemisbn = null;     xdocument xdoc = new xdocument();     xdoc = xdocument.parse(xml);         string itemname = xdoc.element("classify").descendants("work").where(s => s.attributes() == "title");     string itemauth = xdoc.element("classify").descendants("work").attributes("author").tostring();     string itemisbn = xdoc.element("classify").descendants("input").tostring();     txttitle.text = itemname;     txtauth.text = itemauth;     txtisbn.text = itemisbn;     string itemddc = xdoc.element("ddc").descendants("latestedition").attributes("sfa"); } 

edit duplicate: link mentioned duplicate not same/similar xml string. uses different approach didn't work mentioned in start of question.

since xml document has namespace defined, apis require qualify element names namespaces. code using "local" names. there couple ways qualify them. example below gets 'title' attribute. simple expand on whatever other content/attributes want.

using system.xml; using system.xml.linq; using system.xml.xpath;  public class program {     public static void main(string[] args)     {         var doc = xdocument.load("http://classify.oclc.org/classify2/classify?stdnbr=9780754073963");         var nsmanager = new xmlnamespacemanager(new nametable());         nsmanager.addnamespace("c", "http://classify.oclc.org");          var work = doc.xpathselectelement("//c:work", nsmanager);         console.writeline(work.attribute("title").value);          var mostpopular = doc.xpathselectelement("//c:ddc/c:mostpopular", nsmanager);         console.writeline(mostpopular.attribute("nsfa").value);                 } } 

Comments

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -