LINQ to XML 查询 - 代码仅返回第一项
我花了相当多的时间阅读和学习 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要获取项目元素的所有后代联系人,然后为每个元素选择一个新的项目联系人。您可以按如下方式更改查询,它将按您的预期工作:(
我会使用
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:
(I would use
xml.Element("whatever").Value
instead ofxml.Element("whatever")
though. Just looks nicer.)试试这个
Try this