1

私の最後の質問によると、 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" に変更すると、0 部屋が返されます。何度も確認しましたが、要素に英数字の値を使用することに問題はありますか?

4

1 に答える 1

2

{1}をアポストロフィで囲みます。

String.Format(
    "//Department[id={0}]/Section[SectionId='{1}']/Room",
    departmentId,
    sectionId))
于 2013-08-17T06:43:04.320 に答える