c# - Getting data from an xml file based on array from another query from another xml file -
i have xml file table student , below structure.
<?xml version="1.0" encoding="utf-8"?> <studentmodules> <student student_id="001"> <module id="m001" /> <module id="m002" /> <module id="m003" /> <module id="m004" /> </student> <student student_id="002"> <module id="m005"/> <module id="m006" /> <module id="m007"/> <module id="m008" /> </student>
then have file modules , below structure
<?xml version="1.0" encoding="utf-8"?> <moduleschedule> <moduletimetable module_id="m001" modulename="module name 1"> <slot day="monday" time="09:30"/> <slot day="tuesday" time="14:30"/> <slot day="fridayday" time="09:30"/> <slot day="saturday" time="12:30"/> </moduletimetable> <moduletimetable module_id="m002" modulename="module name 2"> <slot day="monday" time="09:30"/> <slot day="tuesday" time="14:30"/> <slot day="fridayday" time="09:30"/> <slot day="saturday" time="12:30"/> </moduletimetable> <moduletimetable module_id="m003" modulename="module name 3"> <slot day="monday" time="09:30"/> <slot day="tuesday" time="14:30"/> <slot day="fridayday" time="09:30"/> <slot day="saturday" time="12:30"/> </moduletimetable>
i want use first xml file modules (module id) student_id
example 001. , them use results second query supposed module names module id array result one.
here have,
// first linq query xdocument stdoc = xdocument.load(@"e:\studentmodules.xml"); var studm = (from item in stdoc.descendants("student") item.attribute("student_id").value.equals("001") select item); foreach (xelement n in studm) { var result = (from node in n.descendants() select new { mod_id = node.attribute("id").value }); } //second query (doesnt job) xdocument doc = xdocument.load(@"e:\module_schedule.xml"); var items = item in doc.descendants("moduletimetable") item.attribute("module_id").value.contains("result")// doesnt work select new { moduleid = (string)item.attribute("module_id").value, modulename = (string)item.attribute("modulename").value }; gridview1.datasource = items.tolist(); gridview1.databind();
how can change work. want module name , ids from module_schedule.xml
have same id in array ids returned first xml-file.
//edit @ moment returns empty gridview, no errors. think problem how can call first variable var result in second query
so in code block:
foreach (xelement n in studm) { var result = node in n.descendants() select new { mod_id = node.attribute("id").value }; }
you keep assigning result, not defined within foreach
loop, not outside it, never save or result variable. it's both not available in scope outside of loop, , being overwritten after every iteration of loop.
this should work:
var students = xdocument.load(@"c:\users\keoki\desktop\students.xml"); var modules = xdocument.load(@"c:\users\keoki\desktop\modules.xml"); var items = s in students.descendants("student") s.attribute("student_id").value == "001" select s.descendants().attributes("id").select(a => a.value) ids m in modules.descendants("moduletimetable") ids.contains(m.attribute("module_id").value) select new { moduleid = m.attribute("module_id").value, modulename = m.attribute("modulename").value };
Comments
Post a Comment