Ghooti Farangi
Ghooti Farangi

Reputation: 20156

Exception during the generation Rss by XDocument

I'm using XDocument to create a RSS same below code:

            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")));

with some contents an exception occurring during the execution of this code.

'.', hexadecimal value 0x00, is an invalid character.
'', hexadecimal value 0x1D, is an invalid character.
'', hexadecimal value 0x0B, is an invalid character.

Upvotes: 0

Views: 143

Answers (1)

qwr
qwr

Reputation: 3670

You should sanitize xml data here is what characters can be used inside http://en.wikipedia.org/wiki/Valid_characters_in_XML valid characters will be

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();
}

Upvotes: 2

Related Questions