LINQ to XML 查询 - 代码仅返回第一项

发布于 2024-09-25 02:44:35 字数 2026 浏览 3 评论 0原文

我花了相当多的时间阅读和学习 LINQ to XML,但我在这里遇到了障碍。下面是我的 XML 文件的示例:

<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>

在我的程序中,有人将从下拉列表中选择一个project_number。当他们这样做时,它将触发对 XML 文件的查询,该文件将获取该项目编号,并查找所有合同。

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 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

人疚 2024-10-02 02:44:35

您需要获取项目元素的所有后代联系人,然后为每个元素选择一个新的项目联系人。您可以按如下方式更改查询,它将按您的预期工作:(

        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")
                    };

我会使用 xml.Element("whatever").Value 而不是 xml.Element("whatever")< /code> 不过看起来更好。)

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.)

ㄖ落Θ余辉 2024-10-02 02:44:35

试试这个

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")
                     };

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")
                     };
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文