首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >XDocument生成Rss时出现异常

XDocument生成Rss时出现异常
EN

Stack Overflow用户
提问于 2013-06-10 16:55:12
回答 1查看 138关注 0票数 0

我正在使用XDocument创建一个RSS,与下面的代码相同:

代码语言:javascript
复制
            var rss = new XDocument(
            new XDeclaration("1.0", "utf-8", null),
            new XElement("rss", new XElement("channel",
                                        new XElement("title", blog.Title),
                                        new XElement("link", link),
                                        new XElement("description", blog.Description),
                                        new XElement("generator", "RSS Generated by x"),
                                        new XElement("language", "fa"),
                                        from item in items
                                        select new XElement("item",
                                                            new XElement("title", item.Title),
                                                            new XElement("link", item.Link),
                                                            new XElement("pubDate", item.PubDate),
                                                            new XElement("author", item.Author),
                                                            new XElement("guid", item.Guid),
                                                            new XElement("description", item.Description),
                                                            new XElement("comments", item.Comments)
                                            )
                                ),
                            new XAttribute("version", "2.0")));

对于某些内容,在执行此代码的过程中会发生异常。

代码语言:javascript
复制
'.', hexadecimal value 0x00, is an invalid character.
'', hexadecimal value 0x1D, is an invalid character.
'', hexadecimal value 0x0B, is an invalid character.
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-06-10 17:01:09

您应该清理xml数据,这里是http://en.wikipedia.org/wiki/Valid_characters_in_XML内部可以使用的字符有效字符将是

代码语言:javascript
复制
public string sanitizeText(string sanitize){
    StringBuilder result = new  StringBuilder();
    foreach(   char x in sanitize){
        if ((x == 0x9 || x == 0xA || x == 0xD) ||   
        ((x >= 0x20) && (x <= 0xD7FF)) ||   
        ((x >= 0xE000) && (x <= 0xFFFD)) ||   
        ((x >= 0x10000) && (x <= 0x10FFFF)))  result.Append(x);
    } 
    return result.ToString();
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17020150

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档