c# - Select multiple elements with Linq to XML -
i have c# application , need extract multiple elements linq xml collection.
i have following extract xml file
<sns> <uniquesystem><system>49</system><label>engines</label> <uniquesubsystem><subsystem>30</subsystem><label>apu</label> <uniqueunit><unit>00</unit><label>starter</label> </uniqueunit> </uniquesubsystem> </uniquesystem> <uniquesystem><system>50</system><label>hydraulics</label> <uniquesubsystem><subsystem>30</subsystem><label>reservoir</label> <uniqueunit><unit>00</unit><label>pump</label> </uniqueunit> </uniquesubsystem> </uniquesystem></sns>
i need extract values within each 'uniquesystem' element. in example above, under 'sns' element there 2 'uniquesystem' elements, , within each of these there 'uniquesubsystem' elements , 'uniqueunit' elements each 'label' elements. need extract data build treeview.
my problem extracting multiple elements using linq. how do this?
at moment have
var item = items in doc.descendants("sns").descendants("uniquesystem").descendants("system") orderby items.value select items.descendants("uniquesystem");
i think give me collection of 'uniquesystem' elements, need extract values of multiple elements within. can please help?
my next attmpt follows, giving me null reference exception:
var item = items in doc.descendants("sns").descendants("uniquesystem").descendants("system") orderby items.value select items.descendants("uniquesystem"); foreach (var e in item) { string sys = e.descendants("system").firstordefault().value; string syslabel = e.descendants("system").descendants("label").firstordefault().value; string subsys = e.descendants("subsystem").firstordefault().value; string subsyslabel = e.descendants("subsystem").descendants("label").firstordefault().value; string unit = e.descendants("unit").firstordefault().value; string unitlabel = e.descendants("unit").descendants("label").firstordefault().value; buildsystemnodes(sys, syslabel); //getsubsystems(myitem); }
so code have written below generate tree view. treeview class using in namespace system.web.ui.webcontrols. function o(n^3).
public void processxml(string document) { var doc = xdocument.parse(document, loadoptions.none); var uniquesystemlist = doc.element("sns").elements(); var treeview = new treeview(); string value = string.empty; string text = string.empty; foreach (var uniquesystem in uniquesystemlist) { value = uniquesystem.element("label").value.tostring(); text = uniquesystem.element("system").value.tostring(); var uniquesystemnode = new treenode(text, value); var uniquesubsystemlist = uniquesystem.elements("uniquesubsystem"); foreach (var uniquesubsystem in uniquesubsystemlist) { value = uniquesubsystem.element("label").value.tostring(); text = uniquesubsystem.element("subsystem").value.tostring(); var uniquesubsystemnode = new treenode(text, value); var uniqueunitlist = uniquesubsystem.elements("uniqueunit"); foreach (var uniqueunit in uniqueunitlist) { value = uniqueunit.element("label").value.tostring(); text = uniqueunit.element("unit").value.tostring(); var uniqueunitnode = new treenode(text, value); uniquesubsystemnode.childnodes.add(uniqueunitnode); } uniquesystemnode.childnodes.add(uniquesubsystemnode); } treeview.nodes.add(uniquesystemnode); } }
Comments
Post a Comment