XML parsing error: Switch from current encoding to specified encoding not supported

  • I am saving the contents of a form to a SQL database using XML. The method I was using worked OK with Net 1.1 but it does not with Net 2.0.

    I get the error message: XML parsing error: Switch from current encoding to specified encoding not supported.

    I take the details from the form a populate an instance of a class. I then process this and save it a SQL 2000 stored procedure.

    An extract of the data loading class is:

    namespace RTT.Apps.Website.BLL

    {

    [XmlRoot ("pledges")]

    ///

    /// Summary description for pledge.

    ///

    public class pledge

    {

    private string strName;

    private string strAddress1;

    private string strAddress2;

    private string strTown;

    private string strPostcode;

    private string strEmail;

    private string strTelephone;

    private int intAmount;

    private int intGiftAid;

    private string strMessage;

    [XmlAttribute (AttributeName = "name")]

    public string Name {

    get {

    return this.strName;

    }

    set {

    strName=value;

    }

    }

    [XmlAttribute (AttributeName = "address1")]

    public string Address1 {

    get {

    return this.strAddress1;

    }

    set {

    strAddress1=value;

    }

    }

    From the process class:

    private String UTF8ByteArrayToString(Byte[] characters) {

    UTF8Encoding encoding = new UTF8Encoding();

    String constructedString = encoding.GetString(characters);

    return (constructedString);

    }

    public void sponsorship(pledge details) {

    MemoryStream memoryStream = new MemoryStream();

    XmlSerializer serlizer = new XmlSerializer(typeof(pledge));

    SqlCommand cmdInsert;

    SqlParameter paramReturnValue;

    string strXML = null;

    int intSuccess;

    try {

    XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);

    serlizer.Serialize(xmlTextWriter, details);

    memoryStream = (MemoryStream)xmlTextWriter.BaseStream;

    strXML = UTF8ByteArrayToString(memoryStream.ToArray());

    }catch (Exception e) {

    System.Console.WriteLine(e);

    }

    //// see output at this point further on

    SqlConnection conPledge = new SqlConnection(Settings.GetConfigValues("appSettings","databaseConnectionString"));

    cmdInsert = new SqlCommand("p_xmlPledgeInsert",conPledge);

    cmdInsert.CommandType = CommandType.StoredProcedure;

    cmdInsert.Parameters.Add("@strXML",SqlDbType.NText);

    cmdInsert.Parameters["@strXML"].Value=strXML;

    paramReturnValue = cmdInsert.Parameters.Add("ReturnValue", SqlDbType.Int);

    paramReturnValue.Direction=ParameterDirection.ReturnValue;

    conPledge.Open();

    cmdInsert.ExecuteNonQuery();

    intSuccess = Convert.ToInt16(cmdInsert.Parameters["ReturnValue"].Value);

    conPledge.Close();

    }

    Example output sent to the SQL code:

    Has anybody any idea what my problem can be.


    Robert T Turner

  • I am not sure but if encoding in your code is causing the error you can encode at the top and you can also encode by saving the file as Unicode.  Try the link below for more encoding options. Another thing I did not see in your code why you need the Memory stream so you may get stable results with the XML Document class. Hope this helps.

    http://www.aspnetresources.com/blog/unicode_in_vsnet.aspx

    Kind regards,

    Gift Peddie

    Kind regards,
    Gift Peddie

  • I have three links that deals with your error message.  Hope this helps.

    http://support.microsoft.com/kb/q275883/

    http://www.forum4designers.com/archive25-2005-1-179722.html

    http://www.w3schools.com/xml/xml_encoding.asp

    Kind regards,

    Gift Peddie

    Kind regards,
    Gift Peddie

  • Thanks for your imput but I have found the solution; change all occurances of UTF8 to unicode. When I was trying it out first of all I forgot to change the UTF8 value in the declaration for XmlTextWriter so I was still getting the error (good job I am getting my eyes tested on Friday!)


    Robert T Turner

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply