2014-0430

The ':' character, hexadecimal value 0x3A

作者: momy 分类: 编程开发 0 Comment »
摘要:操作XML时报错:The ':' character, hexadecimal value 0x3A, cannot be included in a name.

操作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>


标签: xml sitemap XElement XNamespace XAttribute 阅读: 7656
上一篇: C# List使用District去重复数据 - 11680次
下一篇: The type or namespace name 'Schema' does not exist - 6436次

向右滑动解锁留言