1

私はこの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>  

これは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();

私の問題は、リストに1つの部屋しかないことですが、2つ取得する必要があります。これがLINQ to xmlを使用する正しい方法であるかどうかもアドバイスしてください。私はLINQにかなり慣れていません。

4

5 に答える 5

2

クエリを次のように変更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");

これで2部屋分になります。

コードが機能しないのはなぜですか?

  1. select el.Element("Section")内の最初のsection要素のみを選択しますDepartment- あなたは決して部屋を得ることができませsectionid == "001002"
  2. select el.Element("Room")in roomsquery は、一致したすべてのセクションから最初の部屋のみを返します。

構文ベースのクエリを機能させるために、呼び出しを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();
于 2013-08-16T08:46:51.830 に答える
2

他の回答の代わりに、Extensions.XPathSelectElements Method (XNode, String)を使用できます(必ず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();

それは自分の好みの問題です。これは書くのが短く、読みやすいと思います。

于 2013-08-16T09:32:29.413 に答える
1

猫の皮を剥ぐ方法がたくさんあることを示すために:

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();
于 2013-08-16T10:11:51.013 に答える