[英]LINQ to XML query returning wrong data
I have this xml 我有这个XML
<?xml version="1.0" encoding="utf-8" ?>
<Departments>
<Department>
<id>001</id>
<Section>
<SectionId>001001</SectionId>
<Room>
<RoomID>001001001</RoomID>
<Owner>guest1</Owner>
</Room>
<Room>
<RoomID>001001002</RoomID>
<Owner>guest11</Owner>
</Room>
</Section>
<Section>
<SectionId>001002</SectionId>
<Room>
<RoomID>001002001</RoomID>
<Owner>guest2</Owner>
</Room>
</Section>
</Department>
</Departments>
and this is my code using Linq to Xml 这是我使用Linq to Xml的代码
var xDoc = XDocument.Load(inputUrl);
var sections = from el in xDoc.Descendants("Department")
where el.Element("id").Value.Equals("001")
select el.Element("Section");
var rooms = from el in sections
where el.Element("SectionId").Value.Equals("001001")
select el.Element("Room");
var roomsList = (from el in rooms
select new Room
{
roomID = (string)el.Element("RoomID"),
owner = (string)el.Element("Owner")
}).ToList();
My problem is I only get 1 room in the List,but I should get two.Please also advice if this is the right way of using LINQ to xml,I am fairly new to LINQ. 我的问题是列表中只有1个房间,但我应该有2个房间。还请告知这是否是使用LINQ到xml的正确方法,我对LINQ还是很陌生。
Change sections
and rooms
queries to: 将
sections
和rooms
查询更改为:
var sections = xDoc.Descendants("Department")
.FirstOrDefault(x => (string)x.Element("id") == "001")
.Elements("Section");
var rooms = sections.Where(x => (string)x.Element("SectionId") == "001001")
.Elements("Room");
With these, you'll get 2 rooms. 有了这些,您将获得2个房间。
Why isn't your code working? 为什么您的代码不起作用?
select el.Element("Section")
selects only first section
element within Department
- you'd never be able to get room from section
with id == "001002"
select el.Element("Section")
只选择第一section
中的元素Department
-你永远无法从获得的房间section
与id == "001002"
select el.Element("Room")
in rooms
query returns only first room from every matched section. rooms
查询中select el.Element("Room")
在每个匹配部分中仅返回第一个房间。 You can change Element
to Elements
and add additional SelectMany(x => x)
calls to make your syntax-based queries work: 您可以将
Element
更改为Elements
并添加其他SelectMany(x => x)
调用,以使基于语法的查询正常工作:
var sections = from el in xDoc.Descendants("Department")
where el.Element("id").Value.Equals("001")
select el.Elements("Section");
var rooms = from el in sections.SelectMany(x => x)
where el.Element("SectionId").Value.Equals("001001")
select el.Elements("Room");
var roomsList = (from el in rooms.SelectMany(x => x)
select new
{
roomID = (string)el.Element("RoomID"),
owner = (string)el.Element("Owner")
}).ToList();
As an alternative to the other answers, you could use the Extensions.XPathSelectElements Method (XNode, String) (make sure you add the using System.Xml.XPath
directive to the top of your file): 作为其他答案的替代方法 ,您可以使用Extensions.XPathSelectElements方法(XNode,字符串) (确保将
using System.Xml.XPath
指令添加到文件顶部):
string
departmentId = "001",
sectionId = "001001";
var xDoc = XDocument.Load(inputUrl);
var rooms = xDoc.XPathSelectElements(
String.Format(
"//Department[id={0}]/Section[SectionId={1}]/Room",
departmentId,
sectionId))
.Select(el => new Room
{
roomID = (string)el.Element("RoomID"),
owner = (string)el.Element("Owner")
}).ToList();
It's a matter of own preference. 这是自己的偏好问题。 I find this shorter to write and easier to read.
我发现这写起来更短,更容易阅读。
Just to show that there are many ways to skin a cat: 只是为了证明有很多方法可以给猫皮:
var xDoc = XDocument.Load(@"C:\TEST\TEST.XML");
var depts = from e in xDoc.Descendants("Department")
where e.Element("id").Value.Equals("001")
select e;
var sections = from e in depts.Descendants("Section")
where e.Element("SectionId").Value.Equals("001001")
select e;
var rooms = (from e in sections.Descendants("Room")
select new //Room
{
ID = (string)e.Element("RoomID"),
Owner = (string)e.Element("Owner")
}).ToList();
What if you change: 如果您更改:
select el.Element("Room");
to 至
select el.Elements("Room");
You're only selecting one room in the middle query, it should be .Elements(...)
(pay attention to the trailing s
): 您只在中间查询中选择一个房间,它应该是
.Elements(...)
(请注意结尾的s
):
var rooms = from el in sections
where el.Element("SectionId").Value.Equals("001001")
select el.Elements("Room");
The same applies to your section
query. 这同样适用于您的
section
查询。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.