使用Linq从XML填充类

时间:2014-01-17 02:59:44

标签: c# xml linq

我有以下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语句来捕获所有细节,并使用以下类填充List。即清单

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来说没什么新意。

4 个答案:

答案 0 :(得分:3)

尝试:

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

答案 1 :(得分:2)

根据你的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();

答案 2 :(得分:1)

你快到了。

稍微修复你的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的说法,您的应用程序标签仍在您的样本中打开。

答案 3 :(得分:0)

这应该可以胜任。

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