0

我正在尝试从此网址解析谷歌日历事件:http ://www.google.com/calendar/feeds/amchamlva%40gmail.com/public/full ,这是我的代码:

 static IEnumerable<Event> getEntryQuery(XDocument xdoc)
    {
        return from entry in xdoc.Root.Elements().Where(i => i.Name.LocalName == "entry")
               select new Event
               {
                   EventId = entry.Elements().First(i => i.Name.LocalName == "id").Value,
                   Published = DateTime.Parse(entry.Elements().First(i => i.Name.LocalName == "published").Value),
                   Title = entry.Elements().First(i => i.Name.LocalName == "title").Value,
                   Content = entry.Elements().First(i => i.Name.LocalName == "content").Value,
                   Where = entry.Elements().First(i => i.Name.LocalName == "gd:where").FirstAttribute.Value,
                   Link = entry.Elements().First(i => i.Name.LocalName == "link").Attribute("href").Value,

               };
    }

using (StreamReader httpwebStreamReader = new StreamReader(e.Result))
            {
                var results = httpwebStreamReader.ReadToEnd();

                XDocument doc = XDocument.Parse(results);

                System.Diagnostics.Debug.WriteLine(doc);

                var myFeed = getEntryQuery(doc);
                foreach (var feed in myFeed)
                {
                    System.Diagnostics.Debug.WriteLine(feed.Content);

                }

            }

它几乎可以正常工作,除了这个:

Where = entry.Elements().First(i => i.Name.LocalName == "gd:where").FirstAttribute.Value,

我得到一个异常可能是因为它的值是空的,实际上我需要获取 valueString 属性值(例如在这种情况下为“某处”)

<gd:where valueString='Somewhere'/>
4

3 回答 3

2

The local name of <gd:where> is just where - the gd part is the namespace alias.

Rather than using all of these First calls checking the local name, it would be much cleaner if you'd just use the right fully qualified name. For example:

XNamespace gd = "http://schemas.google.com/g/2005";
XNamespace atom = "http://www.w3.org/2005/Atom";

return from entry in xdoc.Root.Elements(gd + "entry")
       select new Event
       {
           EventId = (string) entry.Element(atom + "id"),
           Published = (DateTime) entry.Element(atom + "published"),
           Title = (string) entry.Element(atom + "title"),
           Content = (string) entry.Element(atom + "content"),
           Where = (string) entry.Element(gd + "where")
           Link = (string) entry.Element(atom + "link")
       };

(That's making educated guesses on the namespace based on some documentation. You should check this against your actual feed though.)

于 2013-02-18T13:59:35.970 回答
2

'gd' 看起来像一个命名空间,看看如何在 LINQ to XML 中使用 xml 命名空间:

http://msdn.microsoft.com/en-us/library/bb387093.aspx

http://msdn.microsoft.com/en-us/library/bb669152.aspx

也许尝试一些类似的东西

XNamespace gdns = "some namespace here";
entry.Elements(gdns + "where")
于 2013-02-18T13:57:40.610 回答
2

感谢您的帮助,它适用于这个简单的代码:

   //Where = entry.Elements().First(i => i.Name.LocalName == "where").Value,
   Where = entry.Elements().First(i => i.Name.LocalName == "where").Attribute("valueString").Value,

稍后我将尝试实施您的建议以获得更好的代码实现;)

于 2013-02-18T14:11:26.107 回答