LINQ to XML query - the code only returns the first item -


I have to read and learn a few hours, but I've hit a roadblock here. Here is an example of my XML file:

  & lt; Project & gt; & Lt; Project_number & gt; 20071234 & lt; / Project_number & gt; & Lt; PROJECT_NAME & gt; PROJECTA & lt; / PROJECT_NAME & gt; & Lt; Project_unc & gt; \\ fileserver1 \ Projects \ & lt; / Project_unc & gt; & Lt; Contract & gt; & Lt; Full_name & gt; Contract 00 Project 1 & lt; / Full_name & gt; & Lt; DIRECTORY_NAME & gt; 00_Project1 & lt; / DIRECTORY_NAME & gt; & Lt; / Agreement & gt; & Lt; Contract & gt; & Lt; Full_name & gt; Contract 01 Project 2 & lt; / Full_name & gt; & Lt; DIRECTORY_NAME & gt; 01_Project2 & lt; / DIRECTORY_NAME & gt; & Lt; / Agreement & gt; & Lt; / Project & gt; & Lt; Project & gt; & Lt; Project_number & gt; 20081234 & lt; / Project_number & gt; & Lt; PROJECT_NAME & gt; ProjectB & lt; / PROJECT_NAME & gt; & Lt; Project_unc & gt; \\ fileserver2 \ projects \ & lt; / Project_unc & gt; & Lt; Contract & gt; & Lt; Full_name & gt; Contract 00 Project 3 & lt; / Full_name & gt; & Lt; DIRECTORY_NAME & gt; 00_project3 & lt; / DIRECTORY_NAME & gt; & Lt; / Agreement & gt; & Lt; Contract & gt; & Lt; Full_name & gt; Contract 01 Project 4 & lt; / Full_name & gt; & Lt; DIRECTORY_NAME & gt; 01_project4 & lt; / DIRECTORY_NAME & gt; & Lt; / Agreement & gt; & Lt; / Project & gt;  

One of the programs in my program is going to take Project_Number when they do this, then it will trigger a query on the XML file that receives that project_number, and all contracts Will see

  XDocument XDoc = null; XDoc = XDocument.Load ("projects.xml"); & Lt; ProjectContract & gt; PC = new list & lt; Project Contract & gt; (); //xDoc.Descendants ("project") in xml.example ("project_number") == dropDown1.SelectedItem select new projectContract {fullname = (string) xml.Element ("Entente (" full_name "), dirname = ( String) xml.Element ("contract"). Element ("directory_name"); pc = query.ToList ();  

I'm clearly doing something wrong here; I What can not be seen now is that this code only gives the first contract item from the project, but not both.

All you need to get is the descent of the project element Select the connection and then select a new project contact for each You can change your query as follows and it will work as expected: xml.Descendants ("Project") in xml.xml.xml in xml.Descendants ("contract" ) In

  var query = xml where (string) xml.Element ("project_number") == drop down 1. Selected ITem new project contract {fullname = (string) contactxml Select Element ("full_name"), dirname = (string) contactxml.Element ("directory_name")};  

(I will use xml.Element ("whatever"). instead of xml.Element ("whatever") however Just looks good.)


Comments