Exemplo n.º 1
0
        private SitemapFile ParseSitemapIndex(XmlNode sitemapNode)
        {
            var result = new SitemapFile();

            foreach (XmlNode urlDetail in sitemapNode.ChildNodes)
            {
                var nodeName  = urlDetail.Name.ToLower();
                var nodeValue = urlDetail.InnerText;

                if (nodeName == "loc")
                {
                    Uri tmpUri;
                    if (Uri.TryCreate(nodeValue, UriKind.Absolute, out tmpUri))
                    {
                        result.Location = tmpUri;
                    }
                }
                else if (nodeName == "lastmod")
                {
                    DateTime tmpLastModified;
                    if (DateTime.TryParse(nodeValue, out tmpLastModified))
                    {
                        result.LastModified = tmpLastModified;
                    }
                }
            }
            return(result);
        }
Exemplo n.º 2
0
#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously
        public async Task <SitemapFile> ParseSitemapAsync(TextReader reader, CancellationToken cancellationToken = default)
#pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously
        {
            var       result = new SitemapFile();
            XDocument document;

            try
            {
#if NETSTANDARD2_1
                document = await XDocument.LoadAsync(reader, LoadOptions.None, cancellationToken);
#else
                document = XDocument.Load(reader, LoadOptions.None);
                cancellationToken.ThrowIfCancellationRequested();
#endif
            }
            catch (XmlException)
            {
                return(null);
            }

            foreach (var topNode in document.Elements())
            {
                var nodeName = topNode.Name.LocalName;

                if (nodeName.Equals("urlset", StringComparison.InvariantCultureIgnoreCase))
                {
                    var urls = new List <SitemapEntry>();

                    foreach (var urlNode in topNode.Elements())
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        var sitemapEntry = ParseSitemapEntry(urlNode);
                        urls.Add(sitemapEntry);
                    }

                    result.Urls = urls;
                }
                else if (nodeName.Equals("sitemapindex", StringComparison.InvariantCultureIgnoreCase))
                {
                    var indexedSitemaps = new List <SitemapIndexEntry>();

                    foreach (var sitemapNode in topNode.Elements())
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        var indexedSitemap = ParseSitemapIndex(sitemapNode);
                        indexedSitemaps.Add(indexedSitemap);
                    }

                    result.Sitemaps = indexedSitemaps;
                }
            }

            return(result);
        }
Exemplo n.º 3
0
        public SitemapFile ParseSitemap(string rawSitemap)
        {
            var result   = new SitemapFile();
            var document = new XmlDocument();

            try
            {
                document.LoadXml(rawSitemap);
            }
            catch (XmlException)
            {
                return(null);
            }

            foreach (XmlNode topNode in document.ChildNodes)
            {
                if (topNode.Name.ToLower() == "urlset")
                {
                    var urls = new List <SitemapEntry>();

                    foreach (XmlNode urlNode in topNode.ChildNodes)
                    {
                        var sitemapEntry = ParseSitemapEntry(urlNode);
                        urls.Add(sitemapEntry);
                    }

                    result.Urls = urls;
                }
                else if (topNode.Name.ToLower() == "sitemapindex")
                {
                    var indexedSitemaps = new List <SitemapFile>();

                    foreach (XmlNode sitemapNode in topNode.ChildNodes)
                    {
                        var indexedSitemap = ParseSitemapIndex(sitemapNode);
                        indexedSitemaps.Add(indexedSitemap);
                    }

                    result.Sitemaps = indexedSitemaps;
                }
            }

            return(result);
        }