Greg
Greg

Reputation: 3

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

I have spent quite a few hours reading and learning about LINQ to XML, but I have hit a roadblock here. Here is an example of my XML file:

<project>
    <project_number>20071234</project_number>
    <project_name>ProjectA</project_name>
    <project_unc>\\fileserver1\projects\</project_unc>
    <contract>
        <full_name>Contract 00 Project1</full_name>
        <directory_name>00_Project1</directory_name>
    </contract>
    <contract>
        <full_name>Contract 01 Project2</full_name>
        <directory_name>01_Project2</directory_name>
    </contract>
</project>
<project>
    <project_number>20081234</project_number>
    <project_name>ProjectB</project_name>
    <project_unc>\\fileserver2\projects\</project_unc>
    <contract>
        <full_name>Contract 00 Project3</full_name>
        <directory_name>00_project3</directory_name>
    </contract>
    <contract>
        <full_name>Contract 01 Project4</full_name>
        <directory_name>01_project4</directory_name>
    </contract>
</project>

In my program someone is going to pick a project_number from a dropdown list. When they do this, it will trigger a query on the XML file that will grab that project_number, and look for all contracts.

XDocument XDoc = null;
XDoc = XDocument.Load("projects.xml");
List<ProjectContract> pc = new List<ProjectContract>(); //Created in class
var query = from xml in XDoc.Descendants("project") where (string)xml.Element("project_number") == dropDown1.SelectedItem
             select new ProjectContract
             {
                 fullname = (string)xml.Element("contract").Element("full_name"),
                 dirname = (string)xml.Element("contract").Element("directory_name")
             };
pc = query.ToList();

I'm obviously doing something wrong here; I just can't see what. This code only returns the first contract item from either project, but not both.

Upvotes: 0

Views: 494

Answers (2)

Vinay B R
Vinay B R

Reputation: 8421

Try this

var query = from contract in XDoc.Descendants("contract")
            where contract.Parent.Element("project_number").Value == dropDown1.SelectedItem
            select new ProjectContract
                     {
                         fullname = (string)xml.Element("contract").Element("full_name"),
                         dirname = (string)xml.Element("contract").Element("directory_name")
                     };

Upvotes: 0

jball
jball

Reputation: 25014

You need to get all the descendant contacts of the project element and then select a new project contact for each one. You could change your query as follows and it would work as you expect:

        var query = from xml in XDoc.Descendants("project")
                    from contactxml in xml.Descendants("contract")
                    where (string)xml.Element("project_number") 
                                                  == dropDown1.SelectedItem
                    select new ProjectContract
                    {
                        fullname = (string)contactxml.Element("full_name"),
                        dirname = (string)contactxml.Element("directory_name")
                    };

(I would use xml.Element("whatever").Value instead of xml.Element("whatever") though. Just looks nicer.)

Upvotes: 1

Related Questions