How do I get a list of child elements from XDocument object?

前端 未结 1 642
一向
一向 2021-01-13 18:40

I am trying to get all of the \"video\" elements and their attributes from an XML file that looks like this:



        
相关标签:
1条回答
  • 2021-01-13 19:33

    You want to select Descendants("video"). "videos" appears to be your root entry, of which there is 1 element. The inner elements of videos are what you want to query.

    Example:

    var query = from video in document.Descendants("video")
                select new
                {
                    Title = video.Attribute("title").Value,
                    Path = video.Attribute("path").Value
                };
    

    That gives you an IEnumerable of an anonymous type with two string properties. Otherwise, you could simply select "video" and get an IEnumerable<XElement>, which you would further parse as needed.

    0 讨论(0)
提交回复
热议问题