根据我的最后一个问题,LINQ to XML查询返回错误数据
我正在使用XPath通过以下方式阅读此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>
读取代码是
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();
但是,当我在xml和代码中将sectionId更改为字符串“ str1234”时,它返回零个房间。我检查了很多次,对元素使用字母数字值有问题吗?
尝试{1}
使用撇号将括起来:
String.Format(
"//Department[id={0}]/Section[SectionId='{1}']/Room",
departmentId,
sectionId))
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句