The ':' character, hexadecimal value 0x3A
操作XML时报错:The ':' character, hexadecimal value 0x3A, cannot be included in a name.
相关源代码如下:
XNamespace xnamespace = "http://www.sitemaps.org/schemas/sitemap/0.9"; XNamespace xnamespaceForBaiduMobile = "http://www.baidu.com/schemas/sitemap-mobile/1/"; XElement siteMapForArticles = new XElement(xnamespace + "urlset", new XAttribute(XNamespace.Xmlns + "mobile", xnamespaceForBaiduMobile)); //这里开始循环写数据 var articleClasses = db.Class.ToList(); foreach (var articleClass in articleClasses) { siteMapForArticles.Add(new XElement(xnamespace + "url", new XElement(xnamespace + "loc", Request.Url.Scheme + "://" + Request.Url.Host + (Request.Url.Port == 80 ? "" : ":" + Request.Url.Port) + "/article/list/" + articleClass.ID), new XElement(xnamespaceForBaiduMobile + "mobile", new XAttribute("type", "autoadapt")), new XElement(xnamespace + "lastmod", DateTime.Now.ToUniversalTime()), new XElement(xnamespace + "changefreq", "weekly"), new XElement(xnamespace + "priority", 1.0) )); } siteMapForArticles.Save(path + "sitemapforarticleclasses.xml");
以上代码是生成网站Sitemap的相关代码,错误位置在第12行,以上代码系修正错误可正确运行的代码
生成的xml如下:
<urlset xmlns:mobile="http://www.baidu.com/schemas/sitemap-mobile/1/" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> <url> <loc> http://localhost:58473/resume/details/2 </loc> <mobile:mobile type="autoadapt"/> <lastmod> 2014-04-30T06:30:21.0071167Z </lastmod> <changefreq> daily </changefreq> <priority> 1 </priority> </url> </xmlset>
最近评论