none
select N elements from XElement using LINQ

    Question

  • Hi,

    <Root>
      <Values>
    <Date1>1995-01-01</Date1>
    <Date2>2009-01-01</Date2>
    <Value>111.5</Value>
      </Values>
      <Values>
    <Date1>1995-01-01</Date1>
    <Date2>2009-01-02</Date2>
    <Value>111.5</Value>
      </Values>
      <Values>
    <Date1>1995-01-01</Date1>
    <Date2>2009-01-03</Date2>
    <Value>111.5</Value>
      </Values>
      <Values>
    <Date1>1995-01-02</Date1>
    <Date2>2009-01-06</Date2>
    <Value>111.5</Value>
      </Values>
      <Values>
    <Date1>1995-01-02</Date1>
    <Date2>2009-01-01</Date2>
    <Value>111.5</Value>
      </Values>
    </Root>

    from the above xml I want to extract all Values elements based on count of Date1 elements, say I want to select first two  elements for the Date1 element then the result have excluded 3rd element.

    Regards,

    Kanth

    Monday, September 02, 2013 7:29 AM

Answers

  • Hello,

    Please have a try code like below:

    XDocument doc = XDocument.Load(xmlFile);
                IQueryable<XElement> elements = doc.Elements("Root").AsQueryable();
                var result = from e in elements.Elements("Values")
                             where e.Element("Date1").Value.Contains("1995-01-01")
                             select e;

    And here is a link about how to query xml with LinQ.

    http://msdn.microsoft.com/en-us/library/bb387041.aspx

    If I have misunderstood, please let me know.

    Best Regards.


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.


    Tuesday, September 03, 2013 9:48 AM
  • So if you want tha Values element of the first two Date1 elements, then you can change the query to something like.

    var elements = root.Elements("Values")
                       .Elements("Date1")
                       .Select(x => x.Parent)
                       .Take(2);

    Tuesday, September 03, 2013 12:55 PM

All replies

  • Hi Kanth,

    I am moving your thread into the XML, System.Xml, MSXML and XmlLite Forum for dedicated support. Thanks for your understanding.

    Best Regards,


    Jack Zhai[MSFT]
    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support
    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Tuesday, September 03, 2013 5:11 AM
  • Not sure what you mean by count of Date1 elements, but if you want Date1 element for example from first two Values elements you can use following query.

    var elements = root.Elements("Values")
                                 .Take(2)
                                 .SelectMany(x => x.Elements("Date1"));

    Tuesday, September 03, 2013 5:29 AM
  • Hi,

    I wanted to extract Values elements based on Date1 elements. 1,2,4&5 elements when I want to select first 2 elements for each Date1 and 1 & 4 elements when I say single element for each Date1 value.

    Tuesday, September 03, 2013 6:07 AM
  • Hello,

    Please have a try code like below:

    XDocument doc = XDocument.Load(xmlFile);
                IQueryable<XElement> elements = doc.Elements("Root").AsQueryable();
                var result = from e in elements.Elements("Values")
                             where e.Element("Date1").Value.Contains("1995-01-01")
                             select e;

    And here is a link about how to query xml with LinQ.

    http://msdn.microsoft.com/en-us/library/bb387041.aspx

    If I have misunderstood, please let me know.

    Best Regards.


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.


    Tuesday, September 03, 2013 9:48 AM
  • So if you want tha Values element of the first two Date1 elements, then you can change the query to something like.

    var elements = root.Elements("Values")
                       .Elements("Date1")
                       .Select(x => x.Parent)
                       .Take(2);

    Tuesday, September 03, 2013 12:55 PM