' ', hexadecimal value 0x1F, is an invalid character. Line 1, position 1

Nghia Nguyen picture Nghia Nguyen · Jul 18, 2011 · Viewed 55.2k times · Source

I am trying to read a xml file from the web and parse it out using XDocument. It normally works fine but sometimes it gives me this error for day:

 **' ', hexadecimal value 0x1F, is an invalid character. Line 1, position 1**

I have tried some solutions from Google but they aren't working for VS 2010 Express Windows Phone 7.

There is a solution which replace the 0x1F character to string.empty but my code return a stream which doesn't have replace method.

s = s.Replace(Convert.ToString((byte)0x1F), string.Empty);

Here is my code:

        void webClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
    {
        using (var reader = new StreamReader(e.Result))
        {
            int[] counter = { 1 };  
            string s = reader.ReadToEnd();
            Stream str = e.Result;
       //     s = s.Replace(Convert.ToString((byte)0x1F), string.Empty);
    //        byte[] str = Convert.FromBase64String(s);
     //       Stream memStream = new MemoryStream(str);
            str.Position = 0;
            XDocument xdoc = XDocument.Load(str);                

            var data = from query in xdoc.Descendants("user")
                       select new mobion
                       {
                           index = counter[0]++,
                           avlink = (string)query.Element("user_info").Element("avlink"),
                           nickname = (string)query.Element("user_info").Element("nickname"),
                           track = (string)query.Element("track"),
                           artist = (string)query.Element("artist"),
                       };
            listBox.ItemsSource = data;
        }
    }

XML file: http://music.mobion.vn/api/v1/music/userstop?devid=

Answer

Rebecca picture Rebecca · Jul 20, 2011

0x1f is a Windows control character. It is not valid XML. Your best bet is to replace it.

Instead of using reader.ReadToEnd() (which by the way - for a large file - can use up a lot of memory.. though you can definitely use it) why not try something like:

string input;
while ((input = sr.ReadLine()) != null)
{
    string = string + input.Replace((char)(0x1F), ' ');
}

you can re-convert into a stream if you'd like, to then use as you please.

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

Or else you could keep doing readToEnd() and then clean that string of illegal characters, and convert back to a stream.

Here's a good resource for cleaning illegal characters in your xml - chances are, youll have others as well...

https://seattlesoftware.wordpress.com/tag/hexadecimal-value-0x-is-an-invalid-character/