简体   繁体   English

LINQ to XML to POCO对象

[英]LINQ to XML to POCO object

I've got an XML file that I want to turn in to a list of POCO objects. 我有一个XML文件,我想将其转换为POCO对象列表。

I have the following working code to read the XML and create objects from it. 我有以下工作代码来读取XML并从中创建对象。 I just want to check this is a good way to do this and I'm not missing any tricks. 我只是想检查这是一个很好的方法来做到这一点,我不会错过任何技巧。 In particular with regards to the nested Linq query. 特别是关于嵌套的Linq查询。

XDocument xmlDoc = XDocument.Load(path);
var q = from file in xmlDoc.Descendants("File")
        select new ImportDefinition()
        {
            Name = file.Attribute("Name").Value,
            TypeName = file.Attribute("TypeName").Value,
            ColumnMappings =  
            (
                from map in file.Descendants("ColumnMap") 
                select new ColumnMap() 
                { 
                    DatabaseColumn = new Column()
                    { 
                        Name = map.Element("DatabaseColumn").Attribute("Name").Value
                    }
                }
            ).ToList<ColumnMap>()               
        };
List<ImportDefinition> def = q.ToList<ImportDefinition>();

Thanks 谢谢

In case your POCO objects do not only have string properties, XElement and XAttribute provide a wide selection of conversion operators to other types, including nullables in case the element/attribute doesn't exist. 如果您的POCO对象不仅具有字符串属性,则XElement和XAttribute会为其他类型提供广泛的转换运算符选择,包括在元素/属性不存在的情况下为nullables。

Example: 例:

XDocument xmlDoc = XDocument.Load(path);
var q = from file in xmlDoc.Descendants("File")
        select new ImportDefinition()
        {
            Name         = (string)file.Attribute("Name"),
            TypeName     = (string)file.Attribute("TypeName"),
            Size         = (int)file.Attribute("Size"),
            LastModified = (DateTime?)file.Attribute("LastModified"),
            // ...
        };

Maybe try an explicit conversion 也许尝试一下明确的转换

public class ColumnMap
{
    public static explicit operator ColumnMap(XElement xElem)
    {
        return new ColumnMap()
        {
            DatabaseColumn = new Column()
            {
                Name = xElem.Element("DatabaseColumn").Attribute("Name").Value
            }
        };
    }
}

public class ImportDefinition
{
    public static explicit operator ImportDefinition(XElement xElem)
    {
        return new ImportDefinition() 
        { 
            Name           = (string)xElem.Attribute("Name"), 
            TypeName       = (string)xElem.Attribute("TypeName"), 
            Size           = (int)xElem.Attribute("Size"), 
            LastModified   = (DateTime?)xElem.Attribute("LastModified"), 
            ColumnMappings = xElem.Descendants("ColumnMap").Select(xelem => (ColumnMap)xelem).ToList()
        }; 
    }
}

Then use it like so: 然后像这样使用它:

XDocument xmlDoc = XDocument.Load(path); 
List<ImportDefinition> importDefinitions = xmlDoc.Descendants("File").Select(xElem => (ImportDefinition)xElem).ToList()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM