0

我可以获取变量 xmlDoc 的结果并将其保存到 xml 文档(在磁盘上),然后查询返回结果;但是,当我实时运行它(使用下面的代码)时,查询结果为空。

为什么我的查询不适用于 xmlDoc?有关调试输出“xmlDoc.Root”,请参见下面的屏幕截图。

必须有一种更简洁的方法来创建 XDocument??

System.Net.WebClient wc = new System.Net.WebClient();

wc.Credentials = new System.Net.NetworkCredential("bagee18@gmail.com", "my_password");

XDocument xmlDoc = XDocument.Parse(wc.DownloadString(new Uri("https://mail.google.com/mail/feed/atom")), LoadOptions.None);



var q = (from c in xmlDoc.Descendants("entry")

        select new
        {
            name = c.Element("title").Value,
            url = c.Element("link").Attribute("href").Value,
            email = c.Element("author").Element("email").Value
        }).ToList();

q.Dump();

xmlDoc.Root:

XML 的屏幕截图.

4

1 回答 1

0

考虑命名空间:

XNamespace df = xmlDoc.Root.Name.Namespace;

var q = (from c in xmlDoc.Descendants(df + "entry")

        select new
        {
            name = c.Element(df + "title").Value,
            url = c.Element(df + "link").Attribute("href").Value,
            email = c.Element(df + "author").Element(df + "email").Value
        }).ToList();
于 2012-07-19T16:36:00.107 回答