【发布时间】:2011-03-29 09:47:41
【问题描述】:
我正在尝试查看我在 Facebook 上的所有上过学校的朋友。我使用 FQL 来获取一个 xml 文件,其中包含我所有的朋友及其教育信息。但我不能使用 FQL 只选择那些上过同一所学校的人。因此,我正在尝试使用 Linq to XML 来仅选择我想要的用户。我已经尝试了一些方法,但这些方法对我不起作用。
这是我的 linq 查询:
XDocument doc = RemoveNamespace (XDocument.Load(url));
string[] search = {"Katho", "katho", "kortrijk" , "vhti"};
List<string> keys = new List<string>( search );
var user = (from usr in doc.Descendants("user")
select new fbUser
{
name = usr.Element("name").Value,
email = usr.Element("email").Value,
current_location = StringExtensions.checkNull(usr.Element("current_location").Value,""),
hometown_location = usr.Element("hometown_location").Value,
pic = StringExtensions.checkNull(usr.Element("pic_square").Value,"http://www.fox16.com/media/avatar/default.jpg"),
education_history = (from edu in usr.Descendants("education_info")
//where usr.Element("education_history").HasElements
where keys.Contains(edu.Element("name").Value)
select new Education
{
name = StringExtensions.checkNull(edu.Element("name"),""),
year = StringExtensions.checkNull(edu.Element("year"),""),
school_type = StringExtensions.checkNull(edu.Element("school_type"),""),
concentrations = from conc in edu.Descendants("concentrations")
where (edu.Element("concentrations").HasElements)
select new Concentration
{
name = StringExtensions.checkNull(conc.Element("concentration").Value, "")
}
})
}).Take(5)
;
编辑:可在此处找到示例 xml:sample XML
还有一个问题。学校名称不是来自 fbUser 对象的直接属性。同样在 xml 文件中,学校名称只能在这里找到
<user><education_history><education_info><name>
这是我的 where 语句:
where usr.Element("education_history").Element("education_info").Element(name").value == "schoolname"
问题是 xml 中并不总是有education_history 或education_info 节点。
所以我也需要一种方法来解决这个问题..
非常感谢您对此问题的任何帮助。
希望你能帮帮我!
格子
【问题讨论】:
标签: xml linq facebook select facebook-fql