none
LINQ to XML RRS feed

  • Question

  • How to obtain the value of <one> element in LINQ to XML?

    <root>
      <one>1</one>
      <two>2</two>
    </root>



    .NETVinodh

    Wednesday, July 25, 2012 10:34 AM

Answers

  • Hi NETVinodh;

    The below code snippet should do what you need. The variable results is a List of the values.

    XDocument xdoc = XDocument.Load("Path to XML File/data.xml");
    
    var results = (from one in xdoc.Descendants("one")
                   select one.Value).ToList();

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Wednesday, July 25, 2012 8:25 PM
  • Hi NETVinodh;

    If you do have nodes in the XML document with the same tag name deeper in the document and only are looking for those node at the top level then you can use this query.

    XDocument xdoc = XDocument.Load("Path to XML File/data.xml");
    
    var results = (from one in xdoc.Elements("one")
                   select one.Value).ToList();

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Friday, July 27, 2012 12:51 PM

All replies

  • Try this

      XDocument xdoc = XDocument.Load("XMLFile1.xml"); 
      var lv1s = from lv1 in xdoc.Descendants("one") 
                           select lv1;
    foreach (var lv1 in lv1s)
      {
         Console.WriteLine(lv1.Value);
      }


    With Thanks and Regards
    Sambath Raj.C
    click "Proposed As Answer by" if this post solves your problem or "Vote As Helpful" if a post has been useful to you
    Happy Programming!

    Wednesday, July 25, 2012 11:24 AM
  • Hi NETVinodh;

    The below code snippet should do what you need. The variable results is a List of the values.

    XDocument xdoc = XDocument.Load("Path to XML File/data.xml");
    
    var results = (from one in xdoc.Descendants("one")
                   select one.Value).ToList();

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Wednesday, July 25, 2012 8:25 PM
  • But you must know that the function Descendants also selects all <one> nodes which are deeper in your xml file tree like:

    <root>
      <one>1</one>
      <two>
        <one>this one</one>
      </two>
    </root>

    Thursday, July 26, 2012 7:36 PM
  • But you must know that the function Descendants also selects all <one> nodes which are deeper in your xml file tree like:

    <root>
      <one>1</one>
      <two>
        <one>this one</one>
      </two>
    </root>

    yes Descendants digs deeper into the XML tree. So why don't we take the first level node with the .Elements("") extension method ?

    try,

    	from item in xelm.Elements("one"select item


    With Best Regards, Boovendan M

    Friday, July 27, 2012 9:10 AM
  • Hi NETVinodh;

    If you do have nodes in the XML document with the same tag name deeper in the document and only are looking for those node at the top level then you can use this query.

    XDocument xdoc = XDocument.Load("Path to XML File/data.xml");
    
    var results = (from one in xdoc.Elements("one")
                   select one.Value).ToList();

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Friday, July 27, 2012 12:51 PM