Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. LINQ
  4. linq to XML: multiple node/value queries in a document

linq to XML: multiple node/value queries in a document

Scheduled Pinned Locked Moved LINQ
csharpdatabaselinqxmlquestion
3 Posts 2 Posters 3 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    ezazazel
    wrote on last edited by
    #1

    Hi again! Qnother question comes to my mind and since this is the best place to get an answer, I'm trying it here (hey, that's been a compliment!). So I can query a node/value combination from a XML file:

    public FileInfo ReadXML(FileInfo fInfo, string node, string value) {
    XDocument xmlFile = XDocument.Load(fInfo.FullName);
    var query = (from p in xmlFile.Descendants(node)
    where p.Value.Contains(value)
    select p).ToArray();
    if (query.Count() != 0)
    return fInfo; }

    but how this should look like if I have for instance two node/value combinations I have to query for. E.g: Name has to be Franz AND he needs to be born in Canada Name has to be either Franz or the Person has to be born in CANADA As always thanks in advance, eza (this is a double post, but I missed this forum in the first place)

    H 1 Reply Last reply
    0
    • E ezazazel

      Hi again! Qnother question comes to my mind and since this is the best place to get an answer, I'm trying it here (hey, that's been a compliment!). So I can query a node/value combination from a XML file:

      public FileInfo ReadXML(FileInfo fInfo, string node, string value) {
      XDocument xmlFile = XDocument.Load(fInfo.FullName);
      var query = (from p in xmlFile.Descendants(node)
      where p.Value.Contains(value)
      select p).ToArray();
      if (query.Count() != 0)
      return fInfo; }

      but how this should look like if I have for instance two node/value combinations I have to query for. E.g: Name has to be Franz AND he needs to be born in Canada Name has to be either Franz or the Person has to be born in CANADA As always thanks in advance, eza (this is a double post, but I missed this forum in the first place)

      H Offline
      H Offline
      Howard Richards
      wrote on last edited by
      #2

      In this case you cannot look at the individual descendants but you need to search the "parent" nodes individually, and then query their descendants. Example: given the file

      <?xml version="1.0" encoding="utf-8" ?>
      <people>
      <person>
      <name>Franz</name>
      <country>Canada</country>
      </person>
      <person>
      <name>Franz</name>
      <country>Germany</country>
      </person>
      <person>
      <name>Bob</name>
      <country>Canada</country>
      </person>
      <person>
      <name>Mike</name>
      <country>UK</country>
      </person>
      <person>
      <name>Alan</name>
      <country>Germany</country>
      </person>
      </people>

      First you build a list of the "person" node to get the 'container'. Then you can do a sub-query on each node, looking for the child elements.

              var doc = XDocument.Load("XMLFile1.xml");
      
              // select the nodes of type person
              var nodes = from n in doc.Descendants("person") select n;
      
              // get EITHER Franz or Canada
              var FranzORCanada = from n in nodes
                                  where ((from x in n.Descendants("name") where x.Value == "Franz" select x).Count() > 0)
                                  || ((from y in n.Descendants("country") where y.Value == "Canada" select y).Count() > 0)
                                  select n;
      
              Console.WriteLine("FRANZ or CANADA");
              Console.WriteLine("---------------");
              foreach (var node in FranzORCanada)
              {
                  Console.WriteLine("{0} - {1}", node.Descendants("name").First().Value, node.Descendants("country").First().Value);
              }
              Console.WriteLine();
      
              // get nodes with Franz AND Canada
              var FranzANDCanada = from n in nodes
                                   where ((from x in n.Descendants("name") where x.Value == "Franz" select x).Count() > 0)
                                  && ((from y in n.Descendants("country") where y.Value == "Canada" select y).Count() > 0)
                                  select n;
      
              Console.WriteLine("FRANZ and CANADA");
              Console.WriteLine("----------------");
              foreach (var node in FranzANDCanada)
              {
                  Console.WriteLine("{0} - {1}", node.Descendants("name").Fir
      
      E 1 Reply Last reply
      0
      • H Howard Richards

        In this case you cannot look at the individual descendants but you need to search the "parent" nodes individually, and then query their descendants. Example: given the file

        <?xml version="1.0" encoding="utf-8" ?>
        <people>
        <person>
        <name>Franz</name>
        <country>Canada</country>
        </person>
        <person>
        <name>Franz</name>
        <country>Germany</country>
        </person>
        <person>
        <name>Bob</name>
        <country>Canada</country>
        </person>
        <person>
        <name>Mike</name>
        <country>UK</country>
        </person>
        <person>
        <name>Alan</name>
        <country>Germany</country>
        </person>
        </people>

        First you build a list of the "person" node to get the 'container'. Then you can do a sub-query on each node, looking for the child elements.

                var doc = XDocument.Load("XMLFile1.xml");
        
                // select the nodes of type person
                var nodes = from n in doc.Descendants("person") select n;
        
                // get EITHER Franz or Canada
                var FranzORCanada = from n in nodes
                                    where ((from x in n.Descendants("name") where x.Value == "Franz" select x).Count() > 0)
                                    || ((from y in n.Descendants("country") where y.Value == "Canada" select y).Count() > 0)
                                    select n;
        
                Console.WriteLine("FRANZ or CANADA");
                Console.WriteLine("---------------");
                foreach (var node in FranzORCanada)
                {
                    Console.WriteLine("{0} - {1}", node.Descendants("name").First().Value, node.Descendants("country").First().Value);
                }
                Console.WriteLine();
        
                // get nodes with Franz AND Canada
                var FranzANDCanada = from n in nodes
                                     where ((from x in n.Descendants("name") where x.Value == "Franz" select x).Count() > 0)
                                    && ((from y in n.Descendants("country") where y.Value == "Canada" select y).Count() > 0)
                                    select n;
        
                Console.WriteLine("FRANZ and CANADA");
                Console.WriteLine("----------------");
                foreach (var node in FranzANDCanada)
                {
                    Console.WriteLine("{0} - {1}", node.Descendants("name").Fir
        
        E Offline
        E Offline
        ezazazel
        wrote on last edited by
        #3

        Thank you for your excelent solution.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups