' ', hexadecimal value 0x1F, is an invalid

2020-04-02 06:16发布

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=

9条回答
Luminary・发光体
2楼-- · 2020-04-02 07:01

Works for me.........

string.Replace(Chr(31), "")
查看更多
聊天终结者
3楼-- · 2020-04-02 07:06

I used XmlSerializer to parse XML and faced the same exception. The problem is that the XML string contains HTML codes of invalid characters

This method removes all invalid HTML codes from string (based on this thread - https://forums.asp.net/t/1483793.aspx?Need+a+method+that+removes+illegal+XML+characters+from+a+String):

    public static string RemoveInvalidXmlSubstrs(string xmlStr)
    {
        string pattern = "&#((\\d+)|(x\\S+));";
        Regex regex = new Regex(pattern, RegexOptions.IgnoreCase);
        if (regex.IsMatch(xmlStr))
        {
            xmlStr = regex.Replace(xmlStr, new MatchEvaluator(m =>
            {
                string s = m.Value;
                string unicodeNumStr = s.Substring(2, s.Length - 3);

                int unicodeNum = unicodeNumStr.StartsWith("x") ?
                Convert.ToInt32(unicodeNumStr.Substring(1), 16)
                : Convert.ToInt32(unicodeNumStr);

                //according to https://www.w3.org/TR/xml/#charsets
                if ((unicodeNum == 0x9 || unicodeNum == 0xA || unicodeNum == 0xD) ||
                ((unicodeNum >= 0x20) && (unicodeNum <= 0xD7FF)) ||
                ((unicodeNum >= 0xE000) && (unicodeNum <= 0xFFFD)) ||
                ((unicodeNum >= 0x10000) && (unicodeNum <= 0x10FFFF)))
                {
                    return s;
                }
                else
                {
                    return String.Empty;
                }
            })
            );
        }
        return xmlStr;
    }
查看更多
家丑人穷心不美
4楼-- · 2020-04-02 07:07

I had the same issue and found that the problem was a &#31; embedded in the xml. The solution was:

s = s.Replace("&#31;", " ")
查看更多
登录 后发表回答