简体   繁体   中英

Searching for values in multiple child node using XML and C#

I am having a problem when trying to search in multiple child nodes using xElement. My Sample XML file:

 <VIDEOS> <VIDEO> <ID>1</ID> <NAME>AAA</NAME> <ACTORS> <ACTOR> <NAME>AAA</NAME> <URL>www.aaa.com</URL> </ACTOR> <ACTOR> <NAME>BBB</NAME> <URL>www.bbb.com</URL> </ACTOR> <ACTOR> <NAME>CCC</NAME> <URL>www.ccc.com</URL> </ACTOR> </ACTORS> </VIDEO> <VIDEO> <ID>2</ID> <NAME>BBB</NAME> <ACTORS> <ACTOR> <NAME>AAA</NAME> <URL>www.aaa.com</URL> </ACTOR> <ACTOR> <NAME>DDD</NAME> <URL>www.ddd.com</URL> </ACTOR> <ACTOR> <NAME>EEE</NAME> <URL>www.eee.com</URL> </ACTOR> </ACTORS> </VIDEO> <VIDEO> <ID>3</ID> <NAME>CCC</NAME> <ACTORS> <ACTOR> <NAME>CCC</NAME> <URL>www.ccc.com</URL> </ACTOR> <ACTOR> <NAME>BBB</NAME> <URL>www.bbb.com</URL> </ACTOR> <ACTOR> <NAME>EEE</NAME> <URL>www.eee.com</URL> </ACTOR> </ACTORS> </VIDEO> </VIDEOS>

I tried to use:

var videos = from element in xDocument.Descendants("VIDEO").
    Descendants("ACTORS").
    Descendants("ACTOR").
    Where(e => e.Element("NAME").Value.ToLower().Contains(searchText.ToLower()))
        select new
        {
            Id = element.Element("ID").Value,
            Name = element.Element("NAME").Value,
            Actors = element.Element("ACTORS").Value
        };

Using the following works but I need to search all the actor names and get all the video details from the node. Please help!

var videos = from element in xDocument.Descendants("VIDEO").
    Where(e => e.Element("ACTORS").Value.ToLower().Contains(searchText.ToLower()))
        select new
        {
            Id = element.Element("ID").Value,
            Name = element.Element("NAME").Value,
            Actors = element.Element("ACTORS").Value
        };

Try this code,

static void Main(string[] args)
        {
            string searchText = "eee";
            XDocument xDocument = XDocument.Load(@"test.xml");
            var videos = from element in xDocument.Descendants("VIDEO").
                         Where(e => e.Descendants("ACTORS").
                         Descendants("ACTOR").Any(actor => actor.Element("NAME").Value.ToLower().Contains(searchText.ToLower())))
                         select new
                         {
                             Id = element.Element("ID").Value,
                             Name = element.Element("NAME").Value,
                             Actors = element.Element("ACTORS").Descendants("ACTOR").Select(y => new
                             {
                                 name = y.Element("NAME").Value,
                                 url = y.Element("URL").Value
                             })
                         };
        }

Try code below. If you want a single list you would need to add to end SelectMany().

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication82
{
    class Program
    {
        static void Main(string[] args)
        {
            string xml =
                "<VIDEOS>" +
                    "<VIDEO>" +
                        "<ID>1</ID>" +
                        "<NAME>AAA</NAME>" +
                        "<ACTORS>" +
                            "<ACTOR>" +
                                "<NAME>AAA</NAME>" +
                                "<URL>www.aaa.com</URL>" +
                            "</ACTOR>" +
                            "<ACTOR>" +
                                "<NAME>BBB</NAME>" +
                                "<URL>www.bbb.com</URL>" +
                            "</ACTOR>" +
                            "<ACTOR>" +
                                "<NAME>CCC</NAME>" +
                                "<URL>www.ccc.com</URL>" +
                            "</ACTOR>" +
                        "</ACTORS>" +
                    "</VIDEO>" +
                    "<VIDEO>" +
                        "<ID>2</ID>" +
                        "<NAME>BBB</NAME>" +
                        "<ACTORS>" +
                            "<ACTOR>" +
                                "<NAME>AAA</NAME>" +
                                "<URL>www.aaa.com</URL>" +
                            "</ACTOR>" +
                            "<ACTOR>" +
                                "<NAME>DDD</NAME>" +
                                "<URL>www.ddd.com</URL>" +
                            "</ACTOR>" +
                            "<ACTOR>" +
                                "<NAME>EEE</NAME>" +
                                "<URL>www.eee.com</URL>" +
                            "</ACTOR>" +
                        "</ACTORS>" +
                    "</VIDEO>" +
                    "<VIDEO>" +
                        "<ID>3</ID>" +
                        "<NAME>CCC</NAME>" +
                        "<ACTORS>" +
                            "<ACTOR>" +
                                "<NAME>CCC</NAME>" +
                                "<URL>www.ccc.com</URL>" +
                            "</ACTOR>" +
                            "<ACTOR>" +
                                "<NAME>BBB</NAME>" +
                                "<URL>www.bbb.com</URL>" +
                            "</ACTOR>" +
                            "<ACTOR>" +
                                "<NAME>EEE</NAME>" +
                                "<URL>www.eee.com</URL>" +
                            "</ACTOR>" +
                        "</ACTORS>" +
                    "</VIDEO>" +
                "</VIDEOS>";

            XDocument doc = XDocument.Parse(xml);

            var results = doc.Descendants("VIDEO").Select(x => new {
                id = (int)x.Element("ID"), 
                name = (string)x.Element("NAME"),
                actors = x.Descendants("ACTOR").Select(y => new {
                    name = (string)y.Element("NAME"),
                    url = (string)y.Element("URL")
                }).ToList()
            }).ToList();

        }

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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