Connecting Tech Pros Worldwide Forums | Help | Site Map

Error while loading RSSstream in XmlDocument

Member
 
Join Date: Jun 2009
Location: Bangalore
Posts: 44
#1: Jun 4 '09
Hi All,

I am getting error while am loading RSS Stream in XMLDocument object. The error is as follows.

"System.Xml.XmlException: '', hexadecimal value 0x19, is an invalid character. Line 18, position 32."

I understand that this is becasue I have one apostrophe in the RSS Feed.

I have tried few tricks to get rid of apostrophe but of no use.

Tried the below trick
1. //CONVERTING STREAM TO STRING

StreamReader reader = new StreamReader(rssStream);

2. //Removing ' from the string

without = fortest.Replace("'", "\"");

3. //CONVERTING STRING TO STREAM

byte[] byteArray = Encoding.ASCII.GetBytes(without);
MemoryStream stream = new MemoryStream( byteArray );

4. XmlTextReader reader1 = new XmlTextReader(stream);

5. rssDoc.Load(reader1);

Below is the code wrriten.....

#############################
Expand|Select|Wrap|Line Numbers
  1. System.Net.WebRequest myRequest = System.Net.WebRequest.Create(rssURL);
  2. System.Net.WebResponse myResponse = myRequest.GetResponse();
  3.  
  4. System.IO.Stream rssStream = myResponse.GetResponseStream();
  5. System.Xml.XmlDocument rssDoc = new System.Xml.XmlDocument();
  6.  
  7. //CONVERTING A STREAM TO STRING
  8. StreamReader reader = new StreamReader(rssStream);
  9. fortest = reader.ReadToEnd();
  10.  
  11. //Removing ' from the string
  12. without = fortest.Replace("'", "\"");
  13.  
  14.  
  15. //CONVERTING A STRING TO STREAM
  16. byte[] byteArray = Encoding.ASCII.GetBytes(without); 
  17. MemoryStream stream = new MemoryStream( byteArray );
  18.  
  19. XmlTextReader reader1 = new XmlTextReader(stream);
  20.  
  21.  
  22. try
  23. {
  24. rssDoc.Load(reader1); 
  25. }
  26. catch (System.Xml.XmlException xe)
  27. {
  28. }
  29.  
But its of no use, let me know if you have any idea about it. its lil urgetn

Thanks,
Manik

Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,161
#2: Jun 8 '09

re: Error while loading RSSstream in XmlDocument


0x19 is not an apostrophe, its an unprintable character EM (end of medium)...which really has no use here?
I am more suspect that the file is in utf16 and not ascii
Member
 
Join Date: Jun 2009
Location: Bangalore
Posts: 44
#3: Jun 9 '09

re: Error while loading RSSstream in XmlDocument


Thanks a lot Plater!!

I searched a lot on net to get the solution and finally I got it. Sharing it for others..
Expand|Select|Wrap|Line Numbers
  1. ##########################################################
  2.  
  3. using System;
  4. using System.IO;
  5. using System.Security;
  6. using System.Xml;
  7. using System.Text;
  8. using System.Data;
  9. using System.Configuration;
  10. using System.Web;
  11. using System.Web.Security;
  12. using System.Web.UI;
  13. using System.Web.UI.WebControls;
  14. using System.Web.UI.WebControls.WebParts;
  15. using System.Web.UI.HtmlControls;
  16.  
  17. public partial class _Default : System.Web.UI.Page 
  18. {
  19.     static string rssf;
  20.     static string fortest;
  21.     static string without;
  22.  
  23.     protected void Page_Load(object sender, EventArgs e)
  24.     {
  25.         rssf = null;
  26.         string rssURL = "";
  27.         ProcessRSSItem(rssURL);
  28.     }
  29.  
  30.     public string SanitizeXmlString(string xml)
  31.     {
  32.         if (xml == null)
  33.         {
  34.             throw new ArgumentNullException("xml");
  35.         }
  36.  
  37.         StringBuilder buffer = new StringBuilder(xml.Length);
  38.  
  39.         foreach (char c in xml)
  40.         {
  41.             if (IsLegalXmlChar(c))
  42.             {
  43.                 buffer.Append(c);
  44.             }
  45.         }
  46.  
  47.         return buffer.ToString();
  48.     }
  49.  
  50.     /// <summary>   
  51.     /// Whether a given character is allowed by XML 1.0.   
  52.     /// </summary>   
  53.     public bool IsLegalXmlChar(int character)
  54.     {
  55.         return
  56.         (
  57.              character == 0x9 /* == '\t' == 9   */          ||
  58.              character == 0xA /* == '\n' == 10  */          ||
  59.              character == 0xD /* == '\r' == 13  */          ||
  60.             (character >= 0x20 && character <= 0xD7FF) ||
  61.             (character >= 0xE000 && character <= 0xFFFD) ||
  62.             (character >= 0x10000 && character <= 0x10FFFF)
  63.         );
  64.     }  
  65.     public void ProcessRSSItem(string rssURL)
  66.     {
  67.  
  68.  
  69.  
  70.         System.Net.WebRequest myRequest = System.Net.WebRequest.Create(rssURL);
  71.         System.Net.WebResponse myResponse = myRequest.GetResponse();
  72.  
  73.         System.IO.Stream rssStream = myResponse.GetResponseStream();
  74.         System.Xml.XmlDocument rssDoc = new System.Xml.XmlDocument();
  75.  
  76.         //CONVERTING A STREAM TO STRING
  77.         StreamReader reader = new StreamReader(rssStream);
  78.  
  79.         fortest = reader.ReadToEnd();
  80.  
  81.         //Removing ' from the string
  82.          without = fortest.Replace("'", "\"");
  83.  
  84.  
  85.  
  86.         //CONVERTING A STRING TO STREAM
  87.         byte[] byteArray = Encoding.ASCII.GetBytes(without); 
  88.         MemoryStream stream = new MemoryStream( byteArray );
  89.  
  90.         XmlTextReader reader1 = new XmlTextReader(stream);
  91.  
  92.  
  93.  
  94.  
  95.         //Response.Write(without);
  96.  
  97.  
  98.         try
  99.         {
  100.             rssDoc.LoadXml(SanitizeXmlString(without));  
  101.  
  102.             System.Xml.XmlNodeList rssItems = rssDoc.SelectNodes("rss/channel/item");
  103.  
  104.             string title = "";
  105.             string link = "";
  106.             string description = "";
  107.  
  108.             for (int i = 0; i < rssItems.Count; i++)
  109.             {
  110.                 System.Xml.XmlNode rssDetail;
  111.  
  112.                 rssDetail = rssItems.Item(i).SelectSingleNode("title");
  113.                 if (rssDetail != null)
  114.                 {
  115.                     title = rssDetail.InnerText;
  116.                 }
  117.                 else
  118.                 {
  119.                     title = "";
  120.                 }
  121.  
  122.                 rssDetail = rssItems.Item(i).SelectSingleNode("link");
  123.                 if (rssDetail != null)
  124.                 {
  125.                     link = rssDetail.InnerText;
  126.                 }
  127.                 else
  128.                 {
  129.                     link = "";
  130.                 }
  131.  
  132.                 rssDetail = rssItems.Item(i).SelectSingleNode("pubDate");
  133.                 if (rssDetail != null)
  134.                 {
  135.                     description = rssDetail.InnerText;
  136.                 }
  137.                 else
  138.                 {
  139.                     description = "";
  140.                 }
  141.  
  142.                 // Response.Write("<a href='" + link + "' target='new'>" + title + "</a></b><br/>");
  143.                 rssf = rssf + "<a href='" + link + "' target='new'>" + title + "&nbsp -" + description + "</a>" + "</b><br/><br/>";
  144.                 lblrss.Text = rssf;
  145.  
  146.             }
  147.         }//End try
  148.         catch (System.Xml.XmlException xe)
  149.         {
  150.             //string rawXml = without;
  151.             //string invalidCharsMatch = "(?ims)[\x0\x1\x2\x3\x4\x5\x6\x7\x8\x9\xb\xc\xe\xf" + "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19]";
  152.             //rawXml = System.Text.RegularExpressions.Regex.Replace(rawXml, invalidCharsMatch, "");
  153.             //rssDoc.LoadXml(rawXml);  
  154.  
  155.             lblrss.Text = xe.ToString();
  156.  
  157.         }
  158.  
  159.     }
  160.  
  161. }
  162. ##########################################################
  163.  
Reply