【问题标题】:Best way to create entity objects from xml using LINQ使用 LINQ 从 xml 创建实体对象的最佳方法
【发布时间】:2013-03-13 08:54:50
【问题描述】:

我有以下代码用于从源 XML 创建对象列表。我可以在var query 变量中得到需要的结果。根据该结果创建List<Video> 的最佳方法是什么?

注意:如果可能,首选Method Chaining 方法。

代码

class Program
{
    static void Main(string[] args)
    {
        string xmlStringInput = @"<videoShop>
                                  <video title=""video1"" path=""videos\video1.wma""><Director>Speilberg</Director></video>
                                  <video title=""video2"" path=""videos\video2.wma""/>
                                </videoShop>";

        XDocument myDoc = XDocument.Parse(xmlStringInput);


        var videoElements = (from video in myDoc.Descendants("video") select video).ToList();
        foreach (var videoEle in videoElements)
        {
            //System.Xml.XPath namespace for XPathSelectElement
            var directorName = videoEle.XPathSelectElement(@"Director");
        }


        var query = from video in myDoc.Descendants("video")
                    select new
                    {
                        MyTitle = video.Attribute("title").Value,
                        MyPath = video.Attribute("path").Value
                    };

        //IEnumerable<XElement> elements = (IEnumerable<XElement>)query;
        //List<Video> videoLibrary = (List<Video>)query.ToList<Video>();

        Console.WriteLine(query);
        Console.ReadLine();

    }

}

实体

 public class Video
 {
     public string MyTitle { get; set; }
     public string MyPath { get; set; }
 }

参考:

  1. What's the most efficient way to locate and set element values in an XDocument?
  2. How do I get a list of child elements from XDocument object?
  3. Creating objects from XML
  4. C# LINQ with XML, cannot extract multiple fields with same name into object
  5. How to get XElement's value and not value of all child-nodes?

【问题讨论】:

    标签: c# .net xml linq


    【解决方案1】:
    var query = from vin myDoc.Descendants("video")
                select new Video
                {
                    MyTitle = (string)v.Attribute("title"),
                    MyPath = (string)v.Attribute("path")
                };
    
    // var means List<Video> here
    var results = query.ToList();
    

    或者没有query变量:

    // var means List<Video> here
    var results = (from vin myDoc.Descendants("video")
                   select new Video
                   {
                       MyTitle = (string)v.Attribute("title"),
                       MyPath = (string)v.Attribute("path")
                   }).ToList();
    

    基于方法的查询:

    var results = myDoc.Descendants("video")
                       .Select(v => new Video()
                                    {
                                        MyTitle = (string)v.Attribute("title"),
                                        MyPath = (string)v.Attribute("path")
                                     }).ToList();
    

    【讨论】:

    • 这是由编译器完成的。它检测到 var results 实际上是 List&lt;Video&gt;
    猜你喜欢
    • 2014-01-17
    • 2013-01-22
    • 1970-01-01
    • 2010-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-02
    • 1970-01-01
    相关资源
    最近更新 更多