0

I am a newbie and trying to retrieve all the values of file node based on below xml.

<Changes>
  <Change id="Rest">
    <Name>Restructure</Name>
    <TIDE>
      <Files>
        <File>REGION</File>
      </Files>
    </TIDE>
    <Click>
      <Files>
        <File>DISTRICT</File>
      </Files>
    </Click>
  </Change>
  <Change id="st">
    <Name>New ST</Name>
    <TIDE>
      <Files>
        <File>REGION</File>
      </Files>
    </TIDE>
    <Click>
      <Files>
        <File>DISTRICT</File>
      </Files>
    </Click>
  </Change>
</Changes>

The code I am using is giving me an error "Sequence contains no elements". I have tried to build this code by searching couple of examples on this forum. Can some one help me, much appreciated.

var items = (from i in xmldoc.Root.Elements("Change")
                         where (string)i.Element("Name").Value == listBox1.SelectedValue.ToString()
                         select i).First().Elements("File").ToList();
2
  • Your listBox value have the corresponding value in the xml? Commented Apr 27, 2015 at 4:45
  • Yes, the listbox contain values - Restructure and New St and based on the item selected, I need to populate another listbox. Commented Apr 27, 2015 at 4:55

2 Answers 2

2

This LINQ query returns Change nodes:

(from i in xmldoc.Root.Elements("Change")
 where (string)i.Element("Name").Value == listBox1.SelectedValue.ToString()
 select i)

... and Change nodes don't have direct child node File. You can use Descendants() instead of Elements() for this case.

var items = (from i in xmldoc.Root.Descendants("Change")
             where i.Element("Name").Value == listBox1.SelectedValue.ToString()
             select i).First().Descendants("File").ToList();
Sign up to request clarification or add additional context in comments.

2 Comments

Hi har07, thanks for your response, before posting here, i have even tried with descendants and it too didnt work. Here is the code XDocument xmldoc = XDocument.Load("Change_Files_List.xml"); var items = (from i in xmldoc.Root.Descendants("Change") where (string)i.Element("Name").Value == "Restructure"//listBox1.SelectedValue.ToString() select i).First().Elements("File").ToList();
var items = (from i in xmldoc.Root.Descendants("Change") where (string)i.Element("Name").Value == "Restructure"//listBox1.SelectedValue.ToString() select i).First().Descendants("File").ToList();
1

This error you are getting:

"Sequence contains no elements"

Is beeing throwed by the First() Method call. First() Expects at least one result to be listed, and your filter in Where clause is removing all the results (probably not getting Names correctly from the listbox).

I tested on my machine, replacing the listBox1.SelectedValue.ToString() for "Restructure" and the error doesn't happens anymore.

Even though the exception was not throwed, the result was not as you expected, the items list was empty. To address this other issue you must follow har07 response, and everything will work just fine.

1 Comment

Thanks Lopez, I tried both of yours suggestions and still didnt resolve my problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.