2013-03-15 58 views
0

我有以下XML檢索元素從XML使用LINQ

<Group> 
    <Id>2</Id> 
    <GroupName>Fizzy Drinks</GroupName> 
    <DateCreated>0001-01-01T00:00:00</DateCreated> 
    <DateModified>0001-01-01T00:00:00</DateModified> 
    <Person> 
     <PersonId>78</PersonId> 
     <PersonName>Francesca</PersonName> 
     <PersonSurname>Andrews</PersonSurname> 
     <PersonAge>59</PersonAge> 
    </Person> 
    <Products> 
     <ProductId>2</ProductId> 
     <ProductName>Oranges</ProductName> 
     <CategoryId>4</CategoryId> 
     <CategoryName></CategoryName> 
     <SubCategoryId>7</SubCategoryId> 
     <SubCategoryName>Bread</SubCategoryName> 
    </Products> 
    <Products> 
     <ProductId>12</ProductId> 
     <ProductName>Pepsi</ProductName> 
     <CategoryId>4</CategoryId> 
     <CategoryName></CategoryName> 
     <SubCategoryId>8</SubCategoryId> 
     <SubCategoryName>Dairy</SubCategoryName> 
    </Products> 
    <Products> 
     <ProductId>14</ProductId> 
     <ProductName>Multiwheat Bread</ProductName> 
     <CategoryId>4</CategoryId> 
     <CategoryName></CategoryName> 
     <SubCategoryId>7</SubCategoryId> 
     <SubCategoryName>Bread</SubCategoryName> 
    </Products> 
</Group> 

我希望中檢索組對象中以下。

我有下面的代碼: -

  foreach (XElement xe in xdoc.Descendants("Group")) 
     { 
      int id = Convert.ToInt32(xe.Element("Id").Value); 

      var group = from g in xdoc.Descendants("Group") 
         where (int)g.Element("Id") == id // filtering groups here 
         select new Group 
         { 
          Id = (int)g.Element("Id"), 
          GroupName = (string)g.Element("GroupName"), 
          DateCreated = (DateTime)g.Element("DateCreated"), 
          DateModified = (DateTime)g.Element("DateModified"), 
          Person = from d in g.Descendants("Person") 
            select new Person 
             { 
              Id = (int)d.Element("PersonId"), 
              Name = (string)d.Element("PersonName"), 
              Surname = (string)d.Element("PersonSurname"), 
              Age = (int)d.Element("PersonAge"), 
             }, 

          PersonId = (int)g.Element("PersonId"), 
          PersonName = (string)g.Element("PersonName"), 
          PersonSurname = (string)g.Element("PersonSurname"), 
          PersonAge = (int)g.Element("PersonAge"), 
          Products = g.Elements("Products") 
           .Select(p => new Product 
           { 
            Id = (int)p.Element("ProductId"), 
            ProductName = (string)p.Element("ProductName") 
           }).ToList() 
         }; 

      foreach (var g in group) 
      { 
       GroupList.Add(g); 
      } 

     } 

但是我得到一個錯誤,因爲人不是IEnumerable的。然而,因爲我將只有每個組1人,我不希望使它IEnumerable。有沒有解決的辦法?

感謝您的幫助和時間

回答

0

這聽起來像你可以信息:

Person = (from d in g.Elements("Person") 
      select new Person 
      { 
       Id = (int)d.Element("PersonId"), 
       Name = (string)d.Element("PersonName"), 
       Surname = (string)d.Element("PersonSurname"), 
       Age = (int)d.Element("PersonAge"), 
      }).First() 

,但它會更好,這樣寫:

Person = Person.FromXElement(g.Element("Person")) 

其中FromXElementPerson這樣的靜態方法:

public static Person FromXElement(XElement element) 
{ 
    return new Person 
    { 
     Id = (int) element.Element("PersonId"), 
     Name = (string) element.Element("PersonName"), 
     Surname = (string) element.Element("PersonSurname"), 
     Age = (int) element.Element("PersonAge") 
    }; 
} 

這樣你的查詢將會更清晰。

+0

感謝喬恩我喜歡這個解決方案! – Johann 2013-03-15 12:04:24

0

這是更好地做申報新的範圍變量將舉行<Person>元素並在以後使用它

var group = from g in xdoc.Descendants("Group") 
      let person = g.Element("Person") // here 
      where (int)g.Element("Id") == id 
      select new Group { 
       ... 
      } 

並使用它:

Person = new Person { 
      Id = (int)person.Element("PersonId"), 
      Name = (string)person.Element("PersonName"), 
      Surname = (string)person.Element("PersonSurname"), 
      Age = (int)person.Element("PersonAge") 
      }, 

你可以還要添加null檢查xml中是否有可能沒有人物元素:

Person = (person == null) ? null : 
      new Person { 
       Id = (int)person.Element("PersonId"), 
       Name = (string)person.Element("PersonName"), 
       Surname = (string)person.Element("PersonSurname"), 
       Age = (int)person.Element("PersonAge") 
      },