1

I have following code for creating a list of objects from source XML. I can get the requires result in var query variable. What is the best way to create a List<Video> from this result?

Note: Prefer Method Chaining approach if possible.

CODE

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

    }

}

Entity

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

REFERENCE:

  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?
0

1 Answer 1

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

Or without query variable:

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

Method-based query:

var results = myDoc.Descendants("video")
                   .Select(v => new Video()
                                {
                                    MyTitle = (string)v.Attribute("title"),
                                    MyPath = (string)v.Attribute("path")
                                 }).ToList();
Sign up to request clarification or add additional context in comments.

1 Comment

This is done by compiler. It detects, that var results is actually List<Video>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.