【发布时间】:2011-01-17 21:28:41
【问题描述】:
我目前正在使用 linq 解析移动应用程序的 XML 提要,代码如下:
var Questions = from myQuestion in IXML.Descendants("item")
let Anonymous = myQuestion.Element("asked_by").HasElements == false
let Identifiable = myQuestion.Element("asked_by").HasElements == true
where Identifiable || Anonymous
select new Question
{
AskedQuestion = ParseText.PlainText(myQuestion.Element("question").Value),
ID = Convert.ToInt64(myQuestion.Element("id").Value),
Author_UserName = (Identifiable ? (String)ParseText.PlainText(myQuestion.Element("asked_by").Element("username").Value) : String.Empty),
Author_RealName = (Identifiable ? (String)ParseText.PlainText(myQuestion.Element("asked_by").Element("name").Value.ToLower()) : "anonymous"),
Author_PhotoURL = (Identifiable ? (String)ParseText.PlainText(myQuestion.Element("asked_by").Element("photo_url").Value) : String.Empty)
};
现在,我已改用 JSON 提要,因为它具有较小的下载大小(它是一个移动应用程序)的优势,并且我正在使用 JSON.NET。但是,我似乎无法完全正确地获得 linq 语句?我不是这方面的任何专家,我在任何地方都找不到任何有用的指示,所以这里就是我所拥有的:
var Questions = from m in o["response"].Children()
let Anonymous = m["asked_by"].Values() == null
let Identifiable = m["asked_by"].Values() != null
where Identifiable || Anonymous
select new Question
{
AskedQuestion = ParseText.PlainText((string)m["question"]),
ID = (Int64)m["id"],
Author_UserName = (Identifiable ? (String)ParseText.PlainText((string)m["asked_by"]["username"]) : String.Empty),
Author_RealName = (Identifiable ? (String)ParseText.PlainText((string)m["asked_by"]["name"]) : "anonymous"),
Author_PhotoURL = (Identifiable ? (String)ParseText.PlainText((string)m["asked_by"]["photo_url"]) : String.Empty)
};
不幸的是,这不起作用 - 我假设我正在选择匿名/可识别磨损的条件,但我不知道该怎么做。还有,不知什么原因
ID = (Int64)m["id"]
也不行,
这是我尝试解析的一些 JSON 字符串的 sn-p,如果有帮助的话:/
{"status":"ok","response":[{"id":"3740995599","question":"Have you ever had bumper stickers on your car? If so, what were they?","time":"Mon, 17 Jan 2011 14:44:29 -0500","asked_by":{"username":"ade","name":"Ade Olonoh","website":"http:\/\/about.me\/ade","location":"San Francisco, CA","bio":"Formspring CEO\/Founder","accept_anonymous":true,"protected":false,"photo_url":"http:\/\/files-cdn.formspring.me\/profile\/20100119\/4b56705e1b776_thumb.jpg","answered_count":718,"taking_questions":true,"is_following":true}},{"id":"3710380191","question":"who do you like?? ( :","time":"Sun, 16 Jan 2011 07:10:07 -0500","asked_by":""},{"id":"3708438239" ................"}] }
有什么想法吗?不胜感激,
谢谢大家!
【问题讨论】:
标签: .net xml silverlight linq json