473,407 Members | 2,312 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Error while loading RSSstream in XmlDocument

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
Jun 4 '09 #1
2 4420
Plater
7,872 Expert 4TB
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
Jun 8 '09 #2
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.  
Jun 9 '09 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Kurt Bauer | last post by:
I have an ASP group calendar application which pulls calendar data from Exchange via webdav into an XML string. I then loop the XML nodes to populate a collection of appointments. Finally I use...
0
by: Sonu Kapoor | last post by:
The &amp; is valid in a xml document and the xml string you specified is valid. I checked this in my IE! I dont understand where the error comes from! The code and the xml string you provided should...
1
by: Vishal | last post by:
Hello, I am trying to a load a RSS with some really simple code. However when I run the code I got this error: '', hexadecimal value 0x1F, is an invalid character. Line 1, position 1. ...
0
by: Luis Esteban Valencia | last post by:
Hello I have this error when I submit the page with the following code. I had never see this kind of errors. Thanks private void btnsubmit_Click(object sender, System.EventArgs e) { ...
1
by: Mark | last post by:
I get the following error (see stacktrace) while loading an XMLDocument. The document is huge and it has PI's created from Arbortext's editor. Works fine when I use MSXML2 to load the Document....
2
by: Henrik | last post by:
im reciving an error when i tries to read multidimensional XML data, into my system. I'm receving the same errors discriped at: http://support.microsoft.com/default.aspx?scid=kb;en-us;325695...
0
by: Matthew Copeland | last post by:
A little background. This is a VB.Net client application , and it uses a web reference to a web service, which has been properly refreshed. My app takes collections of two serializable...
4
by: XML newbie: Urgent pls help! | last post by:
I am using VB.Net. My program is to connect to a remote IPAddress. Once, it verifies the login information it should display the SessionID and enable some button . I appreciate your help and thanku...
1
by: =?Utf-8?B?Vmlub2Q=?= | last post by:
Hi, I am having a problem doing the transform. Actually i am migrating the code from XslTranform class into XslCompiledTransform class. Here is the code that i am using in .net 1.1 private...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.