locked
Descendants() and DescendantsAndSelf() RRS feed

  • Question

  • User1904516115 posted

    Why Descendants() and DescendantsAndSelf() returning same result? 

    using System;
    using System.IO;
    using System.Linq;
    using System.Xml.Linq;
    using System.Collections.Generic;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                XElement ele = XElement.Load("Employees.xml");
                //IEnumerable<XElement> elements = ele.Descendants("City");
                IEnumerable<XElement> elements = ele.DescendantsAndSelf("City");
    
                foreach(var item in elements)
                    Console.WriteLine(item);
            }
        }
    }
    //Output
    /* 
    <City>Acampo</City>
    <City>Alta</City>
    <City>Milford</City>
    <City>Montara</City>
    Press any key to continue . . .
    */

    Employees.xml

    <?xml version="1.0" encoding="utf-8" ?>
    <Employees>
     <Employee>
        <EmpId>1</EmpId>
        <Name>Sam</Name>   
        <Sex>Male</Sex>
        <Phone Type="Home">423-555-0124</Phone>
        <Phone Type="Work">424-555-0545</Phone>
       <Address>
          <Street>7A Cox Street</Street>
          <City>Acampo</City>
          <State>CA</State>
          <Zip>95220</Zip>
          <Country>USA</Country>
        </Address>
     </Employee>
     <Employee>
        <EmpId>2</EmpId>
        <Name>Lucy</Name>
        <Sex>Female</Sex>
        <Phone Type="Home">143-555-0763</Phone>
        <Phone Type="Work">434-555-0567</Phone>
        <Address>
          <Street>Jess Bay</Street>
          <City>Alta</City>
          <State>CA</State>
          <Zip>95701</Zip>
          <Country>USA</Country>
        </Address>
     </Employee>
     <Employee>
        <EmpId>3</EmpId>
        <Name>Kate</Name>
        <Sex>Female</Sex>
        <Phone Type="Home">166-555-0231</Phone>
        <Phone Type="Work">233-555-0442</Phone>
        <Address>
          <Street>23 Boxen Street</Street>
          <City>Milford</City>
          <State>CA</State>
          <Zip>96121</Zip>
          <Country>USA</Country>
        </Address>
     </Employee>
     <Employee>
        <EmpId>4</EmpId>
        <Name>Chris</Name>
        <Sex>Male</Sex>
        <Phone Type="Home">564-555-0122</Phone>
        <Phone Type="Work">442-555-0154</Phone>
        <Address>
          <Street>124 Kutbay</Street>
          <City>Montara</City>
          <State>CA</State>
          <Zip>94037</Zip>
          <Country>USA</Country>
        </Address>
     </Employee>
    </Employees>

    Wednesday, February 13, 2019 5:07 AM

Answers

  • User-893317190 posted

    Hi vinodkpasi,

    Only  method DescendantsAndSelf and  Descendants without parameter could return the entire nodes.

    I have made a test.

    My test xml

    <?xml version="1.0" encoding="utf-8" ?> 
    <Employees>
     <Employee>
        <Name>Sam</Name>   
     </Employee>
    </Employees>

    If I write 

      XElement ele = XElement.Load(Server.MapPath("/xmlDemo/employees.xml"));
                
                IEnumerable<XElement> elements = ele.DescendantsAndSelf();
            
                foreach (var item in elements)
                {
                   
                    Response.Write(HttpUtility.HtmlEncode(item));  // HtmlEncode is just to encode the xml , if you are using a console app,you could omit it
                    Response.Write("<br/>");
                }

    The result.

    <Employees>
      <Employee>
        <Name>Sam</Name>
      </Employee>
    </Employees><Employee>
      <Name>Sam</Name>
    </Employee><Name>Sam</Name>

    But if I write

    XElement ele = XElement.Load(Server.MapPath("/xmlDemo/employees.xml"));
               
                IEnumerable<XElement> elements = ele.Descendants();
            
                foreach (var item in elements)
                {
                   
                    Response.Write(HttpUtility.HtmlEncode(item));
                    Response.Write("<br/>");
                }

    I get the result without the Employees node itself.

    <Employee> <Name>Sam</Name> </Employee>
    <Name>Sam</Name>

    If I add parameter to the two methods, it means it want to  filter the result.

    In my case , if I pass Name to the two methods , they will both look for the node whose tagname is Name, so they will both return the Name element.

    If you want to get different result, you should not pass parameter to the two methods or just pass the root element's tag name.

    Best regards,

    Ackerly Xu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, February 14, 2019 2:49 AM