locked
System.Xml.XmlException: Invalid character in the given encoding.

    Question

  • Hello all,

    I need some help regarding this error:

    System.Xml.XmlException: Invalid character in the given encoding.

    I´m trying to load a Xml:

    XmlDocument xDoc = new XmlDocument();
    xDoc.Load("http:// xml Url");

    ... but the xml file have word with ã ç õ, and it´s not defined a encode:

    <Concurso>
    <row>
    <id>124<id>
    <visitante>3746043</visitante>
    <nome>Paulo Guimarães</nome>
    </row>
    </Concurso>

    How can I manage this issue?

    Many thanks.






    Monday, July 21, 2008 8:27 PM

Answers

  • Use the Load(Stream) method instead, where the stream argument is a StreamReader class instance.  Initialize that instance with the StreamReader(String, Encoding) constructor.
    Hans Passant.
    • Marked as answer by Figo Fei Wednesday, July 23, 2008 5:47 AM
    Tuesday, July 22, 2008 4:30 AM

All replies

  • If not specified, the default XML encoding is UTF-8 probably doen's have the characters you are looking for.  The ISO-8859-1 is Western European and may have what you want.  Does your XML file begin with...

        <?xml version="1.0" encoding="utf-8" ?>

    ... if so, try changing it to "ISO-8859-1" and see if it loads.


    Les Potter, Xalnix Corporation
    Monday, July 21, 2008 8:41 PM
  • Nops, It´s not defined ....

    I believe that is the problem, right?
    Is there a way to force the use of ISO-8859-1 encode?

    Thanks
    Tuesday, July 22, 2008 1:17 AM
  • Use the Load(Stream) method instead, where the stream argument is a StreamReader class instance.  Initialize that instance with the StreamReader(String, Encoding) constructor.
    Hans Passant.
    • Marked as answer by Figo Fei Wednesday, July 23, 2008 5:47 AM
    Tuesday, July 22, 2008 4:30 AM
  • 1. If no encoding is specified, xml-readers are supposed to result to UTF-8 in the first place.

    2. Writing on top of your document: encoding='bla bla' doesn't actually SET the encoding on disc.  The program that created the xml-file is responsible for that.  Imagine the 'a' character, in encoding X, on your HardDisk, there will be: 0000 0001.  Where in encoding Y, it might be 000 0010 0000 0110.  Now if the 'a' is part of an xmldocument, and the top of your document says: "encoding=Z", then you will have weird results.  Those weird results are exactly what your problem at hand is.


    The solution(s):
    1. Where does the xml-file come from?  Is it generated by another application you wrote?  Can you change the encoding your application writes in?  Is it a 3rd party (bought, ...) application?  Did you search the help-file for the actual encoding?

    2. Try re-writing the xml-file yourself.  Read it completely, then re-write it in proper encoding.  This worked for me:

    public static void tryApply(string file)
    {
    string path = file;
    UTF8Encoding enc = new UTF8Encoding();
    string[] result = File.ReadAllLines(path, Encoding.Default);
                                                                    //^^
    FileStream fs = new FileStream(path, FileMode.Create);
    BinaryWriter br = new BinaryWriter(fs, Encoding.UTF8);
                                                                   //^^
    for (int t = 0; t < result.Length; t++)
    {
    byte[] buffer = enc.GetBytes(result[t]);
    br.Write(buffer);
    }
    br.Flush();
    br.Close();
    fs.Close();
    }


    The improbable we do, the impossible just takes a little longer. - Steven Parker
    • Proposed as answer by keesb12332432 Tuesday, December 21, 2010 1:19 PM
    Tuesday, July 22, 2008 9:41 AM
  • Is it possible to use StreamReader do read a remote file?
    In my case the xml is provided by a third party ...

    Thanks.
    Tuesday, July 22, 2008 6:40 PM
  • Use the StreamReader(Stream, Encoding) constructor.
    Hans Passant.
    Tuesday, July 22, 2008 6:43 PM
  • Here goes my code:

    <%@ Page Language="C#"%>
    <%@ Import Namespace="System"%>
    <%@ Import Namespace="System.Xml"%>
    <%@ Import Namespace="System.IO" %>

    <%
       
    string path = "website url";
    //string path = @"C:\Inetpub\wwwroot\ninho\web\promocao\teste.xml";
       
    Encoding enc = Encoding.GetEncoding("iso-8859-1");
    StreamReader sr = new StreamReader(path, enc);
    String str = sr.ReadToEnd();

    Response.Write(str);
           
    %>

    If i read a local file it works fine, but when I try to read a remote file I get this erro:

    System.ArgumentException: URI formats are not supported.

    Thanks.

    edudesouza
    Tuesday, July 22, 2008 7:50 PM
  • StreamReader works with streams or files on disk, it doesn't know how to download files from the Internet.  Use something like HttpWebRequest.
    Hans Passant.
    Tuesday, July 22, 2008 7:57 PM
  • Ok, thanks ... I think, I´m almost there:

    string path1 = "xml url";   
    Encoding enc = Encoding.GetEncoding("iso-8859-1");

    WebRequest request = WebRequest.Create(path1);
    request.Credentials = CredentialCache.DefaultCredentials;
    WebResponse response = request.GetResponse();

    Stream dataStream = response.GetResponseStream();
    StreamReader reader = new StreamReader(dataStream, enc);

    string responseFromServer = reader.ReadToEnd();

    reader.Close();
    response.Close();
       
    XmlDocument xDoc = new XmlDocument();
    xDoc.Load(responseFromServer);

    //Response.Write(responseFromServer);

    But I´m receiving tis error:
    System.ArgumentException: Illegal characters in path.


    Thanks
    edudesouza
    Tuesday, July 22, 2008 9:09 PM
  • Use XmlDocument.LoadXml() if you want to do it this way.
    Hans Passant.
    Tuesday, July 22, 2008 9:35 PM
  • Marvelous!
    Works fine, above the code:

    string path1 = " xml url ";
      
    Encoding enc = Encoding.GetEncoding("iso-8859-1"); 
    WebRequest request = WebRequest.Create(path1);
    request.Credentials = CredentialCache.DefaultCredentials;
    WebResponse response = request.GetResponse();

    Stream dataStream = response.GetResponseStream();
    StreamReader reader = new StreamReader(dataStream, enc);

    string responseFromServer = reader.ReadToEnd();

    reader.Close();
    response.Close();

    XmlDocument xDoc = new XmlDocument();
    xDoc.LoadXml(responseFromServer);

    ... you said: "..if you want to do it this way."
    Is there a better way to do all these stuff?

    Many Thanks.

    edudesouza
    • Edited by edu_desouza Tuesday, July 22, 2008 10:06 PM code correction
    • Marked as answer by Figo Fei Wednesday, July 23, 2008 5:47 AM
    • Unmarked as answer by Figo Fei Wednesday, July 23, 2008 5:55 AM
    Tuesday, July 22, 2008 10:01 PM
  • Hi Hans Passant,

                            Excellent. Thanks for the help.

    Regards,

    Zamsheer

    Tuesday, August 31, 2010 9:55 AM
  • Use the Load(Stream) method instead, where the stream argument is a StreamReader class instance.  Initialize that instance with the StreamReader(String, Encoding) constructor.
    Hans Passant.

    Thanks!!
    Tuesday, August 31, 2010 9:57 AM
  • Hi All,

    The ISO-8859-1 encoding also not working for some of the charaters. The application throws error when reading the xml from url using HTTPWebRequest and working fine when I save the same xml to a file manually with the ISO-8859-1 encoding. What could be the issue? I think the characters that create problem are spanish. Please advise.

     

    Regards,

    Zamsheer

    Monday, September 20, 2010 1:43 PM
  • Hi All,

    The ISO-8859-1 encoding also not working for some of the charaters. The application throws error when reading the xml from url using HTTPWebRequest and working fine when I save the same xml to a file manually with the ISO-8859-1 encoding. What could be the issue? I think the characters that create problem are spanish. Please advise.

     

    Regards,

    Zamsheer

    Please start a new thread for your issue.

    Mark the best replies as answers. "Fooling computers since 1971."

    http://rudedog2.spaces.live.com/default.aspx

    Monday, September 20, 2010 7:32 PM
  • Thanks a lot. you saved my day
    Thursday, June 02, 2011 11:13 AM