【问题标题】:xml to class using nested linqxml 到类使用嵌套 linq
【发布时间】:2020-03-25 07:15:17
【问题描述】:

我有一个 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

它用于在加载程序时创建命令层次结构。

现在,我尝试将此结构加载到 UnlinkedCommand 对象列表中,如下所示:

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

CommandReference 看起来像这样:

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

我坚持如何创建一个嵌套的 Linq 查询,该查询可以在查询命令列表时创建子命令列表,我不确定从我读过的关于 Linq 查询的内容中是否有可能。

【问题讨论】:

    标签: c# xml linq linq-to-xml


    【解决方案1】:

    您可以使用Linq进行如下查询

    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()
                                     }
       );
    

    样本输出

    【讨论】:

      【解决方案2】:

      使用 LINQ to XML 查询子元素非常简单。由于在 LINQ 查询中创建包装类时通常位于元素节点,因此在分配给列表时只需查询嵌套元素。使用 LINQ-to-XML 解析此 XML 以获取嵌套元素将如下所示:

      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()
          });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-06-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多