Erik Schulze
Erik Schulze

Reputation: 135

xml to class using nested linq

I have a XML structure, that looks like this:

<Commands>
  <Command id="Prefix" classId="prefix">
    <ShortDescription>A Command</ShortDescription>
    <LongDescription>A Prefix Command</LongDescription>
    <Subcommands>
      <CommandReference commandID="PrefixQuery" syntax="query"></CommandReference>
      <CommandReference commandID="PrefixRevert" syntax="revert"></CommandReference>
      <CommandReference commandID="PrefixSet" syntax="set"></CommandReference>
    </Subcommands>
  </Command>
</Commands

It's used to create a hirarchy of Commands while loading the Programm.

Now, im trying to load this structure into a list of UnlinkedCommand objects, that look like this:

struct UnlinkedCommand
{
  public string commandID;
  public string commandClassID;
  public string shortDescription;
  public string longDescription;
  public List<CommandReference> subcommands;
}

With a CommandReference looking like this:

struct CommandReference
{
  public string commandID;
  public string syntax;
}

Im stuck on how to create a nested Linq Query that can create the List of Subcommands while querying the list of Commands and im not sure if it is even possible from what i've read about Linq queries.

Upvotes: 0

Views: 45

Answers (2)

Jonathon Chase
Jonathon Chase

Reputation: 9704

Querying the sub-elements is pretty straightforward with LINQ to XML. Since you're typically at an element node when creating your wrapping class in the LINQ query, you would simply query the nested elements when assigning to your list. Parsing this XML with LINQ-to-XML to get the nested elements would look something like this:

var xml = @"<Commands>
  <Command id=""Prefix"" classId=""prefix"">
    <ShortDescription>A Command</ShortDescription>
    <LongDescription>A Prefix Command</LongDescription>
    <Subcommands>
      <CommandReference commandID=""PrefixQuery"" syntax=""query""></CommandReference>
      <CommandReference commandID=""PrefixRevert"" syntax=""revert""></CommandReference>
      <CommandReference commandID=""PrefixSet"" syntax=""set""></CommandReference>
    </Subcommands>
  </Command>
</Commands>";

var xdoc = XDocument.Parse(xml);
var commands = xdoc.Elements("Commands").Elements("Command")
    .Select(command => new UnlinkedCommand {
        /* get elements and attributes for other fields */
        subcommands = command.Element("Subcommands")
                             .Elements("CommandReference")
                             .Select(subcommand => new CommandReference { /* assign attributes to fields */ })
                             .ToList()
    });

Upvotes: 0

Anu Viswan
Anu Viswan

Reputation: 18155

You can query using Linq as follows

XElement command = XElement.Parse(xml);
var result = command.Descendants("Command").Select(x=> new UnlinkedCommand
    {
    commandID = x.Attribute("id").Value,
    commandClassID = x.Attribute("classId").Value,
    shortDescription = x.Element("ShortDescription").Value,
    longDescription= x.Element("LongDescription").Value,
    subcommands = x.Element("Subcommands")
                   .Descendants("CommandReference")
                   .Select(c=> new CommandReference
                                 {  
                                   commandID = c.Attribute("commandID").Value, 
                                   syntax = c.Attribute("syntax").Value}).ToList()
                                 }
   );

Sample Output

enter image description here

Upvotes: 2

Related Questions