【问题标题】:Selecting author name field from Atom feed using LINQ (C#)使用 LINQ (C#) 从 Atom 提要中选择作者姓名字段
【发布时间】:2008-11-18 22:28:52
【问题描述】:

我正在尝试使用 LINQ 从 ATOM 提要中的作者节点中选择“名称”字段。我可以像这样获得我需要的所有字段:

XDocument stories = XDocument.Parse(xmlContent);
XNamespace xmlns = "http://www.w3.org/2005/Atom";
var story = from entry in stories.Descendants(xmlns + "entry")
            select new Story
            {
                Title = entry.Element(xmlns + "title").Value,
                Content = entry.Element(xmlns + "content").Value
            };

在这种情况下,我将如何选择作者 -> 姓名字段?

【问题讨论】:

    标签: c# linq atom-feed


    【解决方案1】:

    你基本上想要:

    entry.Element(xmlns + "author").Element(xmlns + "name").Value
    

    但您可能希望将其包装在一个额外的方法中,以便在缺少作者或姓名元素时轻松采取适当的措施。如果有不止一位作者,您可能还想考虑一下您希望发生什么。

    Feed 还可能包含作者元素...只是要记住的另一件事。

    【讨论】:

      【解决方案2】:

      可能是这样的:

              var story = from entry in stories.Descendants(xmlns + "entry")
                          from a in entry.Descendants(xmlns + "author")
                          select new Story
                          {
                              Title = entry.Element(xmlns + "title").Value,
                              Content = entry.Element(xmlns + "subtitle").Value,
                              Author = new AuthorInfo(
                                  a.Element(xmlns + "name").Value,
                                  a.Element(xmlns + "email").Value,
                                  a.Element(xmlns + "uri").Value
                               )
                          };
      

      【讨论】:

      • 我在考虑使用某种嵌套的 LINQ,但不知道该怎么做。我会根据你的建议玩一玩,干杯!
      猜你喜欢
      • 2017-03-05
      • 1970-01-01
      • 1970-01-01
      • 2014-01-11
      • 1970-01-01
      • 1970-01-01
      • 2018-10-22
      • 2022-10-20
      • 1970-01-01
      相关资源
      最近更新 更多