locked
How to get all the Child Elements RRS feed

  • Question

  • HI,

    I have a Requirement that i have to get all the Child nodes as a collection.

    I have a XML like 

    <Interview>

    <Server></Server>

    <Rack></Rack>

    <Server></Server>

    <Rack></Rack>

    <Server></Server>

    <Rack></Rack>

    </Interview>

    I have to get all the Server and Rack in to a Collection ...

    For Getting only Servers i can write a query like xDocument.Descendants("Server") and so on for Rack..

    How can i get both Server and Rack as a Element Collection

    Thanks in Advance,

    Pavan B.V


    Pavan Kumar B.V


    • Edited by bvpavanKumar Monday, September 10, 2012 12:41 PM
    Monday, September 10, 2012 10:53 AM

Answers

  • var res1 = xDocument.Descendants("Server").Concat(xDocument.Descendants("Rack"));
    or
    var res2 = xDocument.Root.Elements().Where(x => x.Name == "Server" || x.Name == "Rack");

    • Marked as answer by bvpavanKumar Tuesday, September 11, 2012 7:15 AM
    Monday, September 10, 2012 7:15 PM

All replies

  • var res1 = xDocument.Descendants("Server").Concat(xDocument.Descendants("Rack"));
    or
    var res2 = xDocument.Root.Elements().Where(x => x.Name == "Server" || x.Name == "Rack");

    • Marked as answer by bvpavanKumar Tuesday, September 11, 2012 7:15 AM
    Monday, September 10, 2012 7:15 PM

  • I just modified your query to 

    var res2 = xDocument.Root.Element("Interview").Elements().Where(x => x.Name.LocalName.Contains("Server") || x.Name.LocalName.Contains("Rack"));

    its working Fine...

    ThanQ Petalvik...


    Pavan Kumar B.V


    • Edited by bvpavanKumar Tuesday, September 11, 2012 7:17 AM
    Tuesday, September 11, 2012 7:15 AM