gpt4 book ai didi

c# - 如何使用 LINQ to XML 从两个 Atom 提要创建聚合 Atom 提要

转载 作者:太空宇宙 更新时间:2023-11-03 16:58:04 26 4
gpt4 key购买 nike

刚刚尝试创建一个聚合提要 - 来自两个独立的 Atom 提要 - 按发布日期降序排列...

更新:感谢 Martin Honnen(MVP)在 http://social.msdn.microsoft.com/Forums/en-US/categories/ - 结合安全的 XElement.Load(url) 帮助程序(和迭代器 block )...我认为下面的代码是聚合 Xml 文档的一种非常好的方法(在这种情况下聚合站点地图 - 尽管这可以很容易地适应 Atom 或RSS 提要)。

下面的命名空间转换助手只转换元素而不转换属性(尽管也可以添加)。

static void Main(string[] args)
{
XDocument feed = MergeSiteMaps(new List<string>() { "http://www.58bits.com/blog/googleSitemap.ashx", "http://www.58bits.com/otherblog/googleSiteMap.ashx", "http://www.58bits.com/photos/sitemap.xml"});

XNamespace sm = "http://www.sitemaps.org/schemas/sitemap/0.9";

foreach (XElement location in feed.Root.Elements(sm + "url").Elements(sm + "loc"))
{
Console.WriteLine((string)location);
}
}

public static XDocument MergeSiteMaps(IEnumerable<string> urls)
{
XNamespace sm = "http://www.sitemaps.org/schemas/sitemap/0.9";
XNamespace xsi = "http://www.w3.org/2001/XMLSchema-instance";
XNamespace xsd = "http://www.w3.org/2001/XMLSchema";
string schemaLocation = "http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd";

//Our container sitemap document
return new XDocument(
new XDeclaration("1.0", "utf-8", "yes"),
new XElement(sm + "urlset",
new XAttribute(XNamespace.Xmlns + "xsi", xsi),
new XAttribute(XNamespace.Xmlns + "xsd", xsd),
new XAttribute(xsi + "schemaLocation", schemaLocation),

new XElement(sm + "url",
new XElement(sm + "loc", "http://www.58bits.com/"),
new XElement(sm + "lastmod", DateTime.Now.ToString("yyyy-MM-dd")),
new XElement(sm + "changefreq", "monthly"),
new XElement(sm + "priority", "1.0")),

new XElement(sm + "url",
new XElement(sm + "loc", "http://www.58bits.com/default.aspx"),
new XElement(sm + "lastmod", DateTime.Now.ToString("yyyy-MM-dd")),
new XElement(sm + "changefreq", "monthly"),
new XElement(sm + "priority", "1.0")),

GetElements(sm, urls, "url"))
);
}

private static IEnumerable<XElement> GetElements(XNamespace ns, IEnumerable<string> urls, string elementLocalName)
{
XElement source;

foreach (string url in urls)
{
try
{
source = XElement.Load(url);
}
catch (Exception ex)
{
//TODO: Log the Url that failed
string message = ex.Message;
continue;
}

XNamespace defaultNamespace = source.GetDefaultNamespace();
bool differentNamespace = (ns != defaultNamespace);
foreach (XElement element in source.Elements(defaultNamespace + elementLocalName))
{
if (differentNamespace)
ChangeNamespace(ns, element);
yield return element;
}
}
}

private static void ChangeNamespace(XNamespace ns, XElement entry)
{
foreach (XElement e in entry.DescendantsAndSelf())
{
if (e.Name.Namespace != XNamespace.None)
{
e.Name = ns.GetName(e.Name.LocalName);
}
}
}

最佳答案

我会查看 SyndicationFeed类(class)。下面是使用它生成提要的示例。

private static SyndicationFeed CreateFeed(List<SyndicationItem> items)
{
// Create the list of syndication items. These correspond to Atom entries
SyndicationFeed feed;

// create the feed containing the syndication items.
feed = new SyndicationFeed()
{
// The feed must have a unique stable URI id
Id = "http://example.com/MyFeed",
Title = new TextSyndicationContent("My Feed"),
Items = items
};
feed.AddSelfLink(WebOperationContext.Current.IncomingRequest.GetRequestUri());
return feed;
}

private static SyndicationItem CreateItem()
{
var item = new SyndicationItem()
{
// Every entry must have a stable unique URI id
Id = id.ToString(),
Title = new TextSyndicationContent(title),
// Every entry should include the last time it was updated
LastUpdatedTime = startDate,
// The Atom spec requires an author for every entry. If the entry has no author, use the empty string
Authors =
{
new SyndicationPerson()
{
Name = author.Name,
Email = author.EmailAddress,
Uri = author.Website
}
},
// The content of an Atom entry can be text, xml, a link or arbitrary content. In this sample text content is used.
Content = new TextSyndicationContent(description),
};

return item;
}

关于c# - 如何使用 LINQ to XML 从两个 Atom 提要创建聚合 Atom 提要,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/668820/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com