我正在尝试使用 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
};
在这种情况下,我将如何选择作者 -> 名称字段?
最佳答案
你基本上想要:
entry.Element(xmlns + "author").Element(xmlns + "name").Value
但是您可能希望将其包装在一个额外的方法中,以便在缺少作者或名称元素时可以轻松地采取适当的措施。如果有多个作者,您可能还想考虑您希望发生什么。
提要也可能有一个作者元素......只是另一件事要记住。
关于c# - 使用 LINQ (C#) 从 Atom 提要中选择作者姓名字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/300376/