c# - MVC Razor view: XML node parsing issue -
i have mvc application querying lucene/solr index. in partial view, displaying metadata on series of xml documents returned query. metadata extracted xml documents using xpath queries , paged, listing 10 documents @ time. view cshtml follows...
@model solrmvctest2.models.facet <section> @foreach (system.xml.xmlnode doc in model.searchresult.selectnodes("/response/result/doc")) { <article> <b>document type: @doc.selectsinglenode("//str[@name='doctype']").innertext </b> <br /> <b>nhs number: @doc.selectsinglenode("//str[@name='crn']").innertext </b> <br /> @doc.outerxml.tostring() </article> <p></p> } </section>
i have included display of outerxml debugging purposes , partial listing of results highlights serious issue. each group of 10 documents, innertext of selected nodes not updated each successive document, displays values first document of set of 10 shown in partial result....
document type: medicalhistory patient number: 7024326654 <doc><str name="id">mh00k2jr001002</str><str name="crn">7024326654</str><str name="doctype">medicalhistory</str></doc> document type: medicalhistory patient number: 7024326654 <doc><str name="id">mh00k2ju001002</str><str name="crn">0437861414</str><str name="doctype">medicalhistory</str></doc> document type: medicalhistory patient number: 7024326654 <doc><str name="id">mh00k2k1001002</str><str name="crn">7626346416</str><str name="doctype">medicalhistory</str></doc> document type: medicalhistory patient number: 7024326654 <doc><str name="id">mh00k2mp001002</str><str name="crn">2637403616</str><str name="doctype">medicalhistory</str></doc> document type: medicalhistory patient number: 7024326654 <doc><str name="id">mh00k2nm001001</str><str name="crn">0874706416</str><str name="doctype">medicalhistory</str></doc> document type: medicalhistory patient number: 7024326654 <doc><str name="id">mh00k2qq001002</str><str name="crn">1867082616</str><str name="doctype">medicalhistory</str></doc> document type: medicalhistory patient number: 7024326654 <doc><str name="id">mh00k2ss001001</str><str name="crn">5436985416</str><str name="doctype">medicalhistory</str></doc> document type: medicalhistory patient number: 7024326654 <doc><str name="id">mh00k2yi002003</str><str name="crn">8731654606</str><str name="doctype">medicalhistory</str></doc> document type: medicalhistory patient number: 7024326654 <doc><str name="id">mh00k2yr001001</str><str name="crn">0906133416</str><str name="doctype">medicalhistory</str></doc> document type: medicalhistory patient number: 7024326654 <doc><str name="id">op00k23e001001</str><str name="crn">6335192616</str><str name="doctype">outpatientannotation</str></doc>
the patient number display (extracted str element named "crn" not updated obn second , successive documents, , document type (fromt doctype element) not updated on 10th.
does have ideas may explain behaviour?
you have behavior, because inner xpath starts double slash //
:
@doc.selectsinglenode("//str[@name='doctype']")
this means global search on xml document, , return first found str
element matches search no matter in document. that's why see data of first doc
element.
use xpath without double slash str
element of current doc
node:
@doc.selectsinglenode("str[@name='doctype']")
also consider following - instead of parsing data in view, create view model, contain required data:
public class document { public string type { get; set; } public string patientnumber { get; set; } }
and create documents in controller
var documents = doc in searchresult.selectnodes("/response/result/doc") select new document { type = doc.selectsinglenode("str[@name='doctype']").innertext, patientnumber = doc.selectsinglenode("str[@name='crn']").innertext }; return view(documents);
Comments
Post a Comment