C# Linq to XML - Parse Nested Object List -
i have xml file format goes (whitespace , ellipses added readability):
<root> <module> //start list of modules <moduleparams> </moduleparams> </module> ... <detectline> //now list of detectlines <detectlineparams> </detectlineparams> <channels> //list of channels embedded in each detectline <channel> <channelparams> </channelparams> </channel> ... </channels> </detectline> ... </root> classes structured follows:
public class module { public moduleparams { get; set; } } public class detectline { public detectlineparams { get; set; } public list<channel> channels { get; set; } } public class channel { public channelparams { get; set; } } the list of modules , detectlines easy parse linq xml. however, parsing channel list each detectline not apparent me. can done linq xml? prefer not having restructure things work xmlserializer.
initial code (openxml openfiledialog. checked filename):
list<module> mymodules; list<detectline> mydetectlines; xdocument config = xdocument.load(openxml.filename); mymodules = (from myelements in config.descendants("module") select new module() { moduleparam1 = (string)myelements.element("moduleparam1"), moduleparam2 = (string)myelements.element("moduleparam2"), ... }).tolist<module>(); mydetectlines = (from myelements in config.descendants("detectline") select new detectline() { detectlineparam1 = (string)myelements.element("moduleparam1"), detectlineparam2 = (string)myelements.element("moduleparam2"), ... // ?? add channels list here somehow... }).tolist<detectline>();
with
xelement detectline = xelement.parse(@"<detectline> <detectlineparams> </detectlineparams> <channels> <channel> <channelparams> </channelparams> </channel> ... </channels> </detectline> "); you can e.g.
detectline dl1 = new detectline() { detectlineparams = ..., channels = (from channel in detectline.element("channels").element("channel") select new channel() { channelparams = new channelparams() { ... = channel.element("channelparams").value } }).tolist(); we need see more of concrete c# class code spell out how set complete query.
[edit] fit in code have posted:
mydetectlines = (from myelements in config.descendants("detectline") select new detectline() { detectlineparam1 = (string)myelements.element("moduleparam1"), detectlineparam2 = (string)myelements.element("moduleparam2"), ... channels = (from channel in myelements.element("channels").element("channel") select new channel() { channelparams = new channelparams() { ... = channel.element("channelparams").value } }).tolist(); }).tolist<detectline>();
Comments
Post a Comment