2010-04-05 36 views
1

我有一個簡單的XML文檔我讀,一個樣本是在這裏:創建使用LINQ複合對象到XML

<people> 
    <person> 
    <name>joe</name> 
    <age>21</age> 
    <contact> 
     <phone-nums> 
     <phone-num> 
      <number>123-4567</number> 
      <type>home</type> 
     </phone-num> 
     <phone-num> 
      <number>123-4567</number> 
      <type>office</type> 
     </phone-num> 
     </phone-nums> 
    </contact> 
    </person> 
</people> 

我使用HttpContent.ReadAsXElement(),然後使用LINQ創建對象閱讀。我簡單的物體看起來像這樣:

public class PeopleList : List<Person> { } 

public class Person 
{ 
    public string name; 
    public int age; 
    public Contact contact; 
} 

public class Contact 
{ 
    public PhoneList phones; 
} 

public class PhoneList : List<Phone>{} 

public class Phone 
{ 
    public string number; 
    public string type; 
} 

好了,現在我有我的課,上面寫着這一切在這其中,我收到掛了(這是在我的代碼擴展方法):

public PeopleList ReadAsPeopleList(this HttpContent content) 
    { 
     var people = content.ReadAsXElement(); 

     var personQuery = from p in people.Elements("person") 
      select new Person() 
      { 
       name = p.Element("name").ValueOrDefault(), 
       age = p.Element("age").ValueOrDefault(), 
       contact = (from c in p.Elements("contact") 
        select new Contact() 
        { 
         //I don't know how to select a new list of phones into a contact here 
        } 
      }; 

     PeopleList l = new PeopleList(); 
     l.AddRange(personQuery); 
     return l; 
    } 

我無法使用複合電話號碼列表創建聯繫人類型。任何幫助,將不勝感激。

注:我改寫了這一切在這裏的一個簡化版本,以便

回答

0

爲了獲得需要的接觸去「電話」的集合,你可以這樣做:

c.Elements("phone-num").Select(phone => new Phone() 
    { 
     number = phone.Element("number").Value, 
     type = phone.Element("type").Value 
    }); 

所以你想

select new Contact() 
{ 
    PhoneList = c.Elements("phone-num").Select(phone => new Phone() 
     { 
      number = phone.Element("number").Value, 
      type = phone.Element("type").Value 
     }) 
} 
0

這個答案將是您的實際問題有點扭曲,但可以提供一些方向的最終解決方案。

考慮使用List<T>作爲您的收藏,而不是創建BusinessObjectCollection : List<T>。這是一個很好的SO閱讀,可能是有趣的:List or BusinessObjectCollection?

這就是說,這是你的類的稍微調整的版本;我已經使用屬性而不是字段。最後,由於我沒有使用HTTPContext,所以我使用基本字符串的例子。這裏介紹的方法應該很容易轉換爲HTTPContext的擴展方法,但是:

public static IEnumerable<Person> ReadAsPeopleList(string xml) 
{ 
    var doc = XDocument.Parse(xml); 

    var people = doc.Root.Elements("person") 
     .Select(x => new Person 
     { 
      Name = x.Element("name").Value, 
      Age = int.Parse(x.Element("age").Value), 
      Contact = new Contact 
      { 
       Phones = x.Descendants("phone-num") 
       .Select(p => new Phone 
       { 
        Number = p.Element("number").Value, 
        Type = p.Element("type").Value 
       }) 
      } 
     } 
     ); 

    return people; 
} 

private static string MyXml = @" 
<people><person><name>joe</name><age>21</age><contact><phone-nums> 
<phone-num><number>123-4567</number><type>home</type></phone-num> 
<phone-num><number>123-4567</number><type>office</type></phone-num> 
</phone-nums></contact></person><person><name>bill</name><age>30</age> 
<contact><phone-nums><phone-num><number>123-4567</number><type>home</type> 
</phone-num><phone-num><number>123-4567</number><type>office</type> 
</phone-num></phone-nums></contact></person></people>";