nopSolutions/nopCommerce · error · Exception

Incorrect file format

Error message

Incorrect file format

What it means

Thrown by the browscap (browser capabilities) XML parser Initialize method when, after trying both the crawlers-only file and the full user-agent file, no crawler items could be loaded. nopCommerce needs at least one browscapitem to build the crawler-detection regex, so an empty/unparseable source is rejected with a generic Exception('Incorrect file format').

Source

Thrown at src/Libraries/Nop.Services/Helpers/BrowscapXmlParser.cs:107

            //try to load crawler list from crawlers only file
            using var sr = new StreamReader(crawlerOnlyUserAgentStringsPath);
            crawlerItems = XDocument.Load(sr).Root?.Elements("browscapitem").ToList();
        }

        if (crawlerItems == null || !crawlerItems.Any())
        {
            //try to load crawler list from full user agents file
            using var sr = new StreamReader(userAgentStringsPath);
            var rootElement = XDocument.Load(sr).Root;
            crawlerItems = rootElement?.Element("browsercapitems")?.Elements("browscapitem")
                //only crawlers
                .Where(IsBrowscapItemIsCrawler).ToList();
            needSaveCrawlerOnly = true;
            comments = rootElement?.Element("comments");
        }

        if (crawlerItems == null || !crawlerItems.Any())
            throw new Exception("Incorrect file format");

        if (_fileProvider.FileExists(additionalCrawlersFilePath))
            crawlerItems.AddRange(GetAdditionalCrawlerItems(additionalCrawlersFilePath));

        var crawlerRegexpPattern = string.Join("|", crawlerItems
            //get only user agent names
            .Select(e => e.Attribute("name"))
            .Where(e => !string.IsNullOrEmpty(e?.Value))
            .Select(e => e.Value)
            .Select(attributeValue =>
            {
                var sb = new StringBuilder(Regex.Escape(attributeValue));
                sb.Replace("&", "&").Replace("\\?", ".").Replace("\\*", ".*?");

                return $"^{sb}$";
            }));

        _crawlerUserAgentsRegexp = new Regex(crawlerRegexpPattern);

View on GitHub (pinned to 64bdf2ff08)

Solutions

  1. Download a fresh, valid browscap.xml from browscap.org and place it in App_Data, then delete browscap.crawlersonly.xml so it is regenerated.
  2. Verify the file is well-formed XML with a <browsercapitems> root containing <browscapitem> elements.
  3. Check file read permissions and that the path configured in BrowscapXmlParser settings is correct.
  4. If the schema changed (crawler detection filter), confirm IsBrowscapItemIsCrawler still matches items in the new browscap version.

Example fix

// before — App_Data/browscap.xml missing or HTML error page saved as XML

// after
// 1. Download the full PHP_BrowsCapINI.xml from https://browscap.org/
// 2. Save as App_Data/browscap.xml
// 3. Delete App_Data/browscap.crawlersonly.xml
// 4. Restart the application so Initialize regenerates the crawlers-only file
Defensive patterns

Strategy: validation

Validate before calling

// Validate the browscap file before parsing
if (!_fileProvider.FileExists(browscapPath))
    throw new FileNotFoundException("browscap.xml not found in App_Data.", browscapPath);
XDocument doc;
try { doc = XDocument.Load(browscapPath); }
catch (Exception) { throw new InvalidOperationException("browscap.xml is not valid XML; re-download from browscap.org."); }
if (doc.Root == null || !doc.Root.Descendants("browscapitem").Any())
    throw new InvalidOperationException("browscap.xml contains no browscapitem elements; re-download a valid file.");

Type guard

static bool IsValidBrowscapXml(XDocument doc)
    => doc?.Root?.Descendants("browscapitem").Any() == true;

Try / catch

try { _browscapParser.Initialize(userAgentPath, crawlerOnlyPath, additionalPath); }
catch (Exception ex) when (ex.Message == "Incorrect file format")
{ /* delete browscap.crawlersonly.xml, re-download browscap.xml from browscap.org, restart */ }

Prevention

When it happens

Trigger: Calling Initialize when both the crawler-only file (browscap.crawlersonly.xml) and the full browscap.xml yield no <browscapitem> crawler elements — e.g. the file is missing, corrupt, an unsupported browscap schema version, or an HTML error page saved as XML.

Common situations: The browscap.xml in App_Data is outdated/empty/corrupt; an automatic browscap download returned an HTML error page; the file uses a newer browscap schema where crawler items are structured differently; file permissions block reading so parsing yields nothing.

Related errors


AI-assisted analysis of nopSolutions/nopCommerce@64bdf2ff08 (2026-08-13). Data as JSON: /api/errors/98bc54cad8df22b0. Report an issue: GitHub.