我有一個簡單的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;
}
我無法使用複合電話號碼列表創建聯繫人類型。任何幫助,將不勝感激。
注:我改寫了這一切在這裏的一個簡化版本,以便