使用 Linq 从 XML 填充类



>我有以下XML

<?xml version="1.0" encoding="utf-8"?>
<Applications   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
                xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Blocks>
    <Block Name="block1">
        <Attributes>
            <Tag>Attribute1</Tag>
            <Layer>layer1</Layer>
        </Attributes>
        <Attributes>
            <Tag>Attribute2</Tag>
            <Layer>layer2</Layer>
        </Attributes>
    </Block>
    <Block Name="block2">
        <Attributes>
            <Tag>Attribute1</Tag>
            <Layer>layer0</Layer>
        </Attributes>
    </Block>
</Blocks>
</Applications>

我想使用 linq 语句来捕获所有详细信息并使用以下类填充列表。

public class Block
{    
public string Tag { get; set; }
    public string Layer { get; set; }
}

我试过了...

List<Block> data =
(from a in xdoc.Root.Elements("Blocks")
where (string)a.Attribute("Name") == "block1"
select new Block
{
    Tag = (string)a.Element("Tag"),
    Layer = (string)a.Element("Layer")
}).ToList();

你能看出我哪里出错了吗,对linq来说有点陌生。

尝试:

LAMBDA 语法:

xdoc.Root.Elements("Blocks").Elements("Block")
    .Where(w => (string)w.Attribute("Name") == "block1")
    .Elements("Attributes")
    .Select(s => new Block
    {
        Tag = (string)s.Element("Tag"),
        Layer = (string)s.Element("Layer")
    });

如果要使用查询语法:

from a in (from b in xdoc.Root.Elements("Blocks").Elements("Block")
        where (string)b.Attribute("Name") == "block1"
        select b).Elements("Attributes")
        select  new Block
        {
            Tag = (string)a.Element("Tag"),
            Layer = (string)a.Element("Layer")
        };

根据您的xml文档,我建议您像这样更改类:

public class Block
{    
   public string Name { get; set; }
   public List<BlockAttribute> Attributes { get; set; }
} 
public class BlockAttribute 
{  
   public string Tag { get; set; }
   public string Layer { get; set; }
}

然后使用以下代码:

var blocks = (from b in xdoc.Descendants("Block")
                 select new Block {
                          Name = (string)b.Attribute("Name"),
                          Attributes = (from a in b.Elements("Attributes")
                                           select new BlockAttribute {
                                                   Tag = (string)a.Element("Tag"),
                                                   Layer = (string)a.Element("Layer")
                                                  }).ToList()
                                    }).ToList();

你快到了。

稍微修正一下 linq 语句:

List<Block> data = (from a in (from b in xdoc.Root.Elements("Blocks").Elements("Block")
                               where b.Attribute("Name").Value.Equals("block1")
                               select b).Elements("Attributes")
                    select new Block()
                    {
                         Tag = a.Element("Attributes").Element("Tag").Value,
                         Layer = a.Element("Attributes").Element("Layer").Value
                    }).ToList();

还要确保您的 XML 有效,因为您混合了大小写。 此外,根据@Grant-Winney提到,您的应用程序标签在您的样本中仍然打开。

这应该可以完成这项工作。

        var blocks = xdoc.Descendants("Attributes").Select(x => new Block
            {
                Tag = x.Descendants("Tag").Single().Value,
                Layer = x.Descendants("Layer").Single().Value
            }).ToList();

最新更新