Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7846719
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T17:40:44+00:00 2026-06-02T17:40:44+00:00

I have a C# Web Service that is serializing my simple class: [Serializable] [XmlInclude(typeof(Bitmap))]

  • 0

I have a C# Web Service that is serializing my simple class:

    [Serializable]
    [XmlInclude(typeof(Bitmap))]
    [XmlTypeAttribute(Namespace = "http://tempuri.org/")]
    public class Class1 
    {
        private static Bitmap _myImage = new Bitmap(@"C:\WebApplication1\ClassLibrary1\Untitled.png");


        public Bitmap MyImage
        {
            get { return _myImage; }

            set
            {
                _myImage = value;
            }
        }
    }

Here’s the asmx.cs code that does the serialization:

[WebMethod]
public string HelloWorld()
{
    var c = new Class1();
    XmlSerializer serializer = new XmlSerializer(typeof(Class1));

    return XMLSerializer(c);
}

public string XMLSerializer(object pObject)
{
    try
    {
        XmlSerializer xs = new XmlSerializer(pObject.GetType());
        using (StringWriter stream = new StringWriter())
        {
            xs.Serialize(stream, pObject);
            stream.Flush();
            return stream.ToString();
        }
    }
    catch (Exception ex)
    {
        return ex.ToString();
    }

}

Looks prety straight forward. However, the XML generated by the XmlSerializer is producing and error when I try to DeSerialize it.

{"There is an error in XML document (5, 5)."}
{"Parameter is not valid."}

When I try to load the generated XML into IE I get this error.

Switch from current encoding to specified encoding not supported. Error processing resource 'file:///C:/Users/mhall15/Deskt...

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

Here’s the generated XML:

<?xml version="1.0" encoding="utf-16"?>
<Class1 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <MyImage xmlns="http://tempuri.org/">
    <Palette />
  </MyImage>
</Class1>

Any ideas what’s going on?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T17:40:46+00:00Added an answer on June 2, 2026 at 5:40 pm

    During the serialization, replace “encoding=”utf-16” with “encoding=”utf-8″” and that will cut it. The source of the problem – I’m not sure, but I’ve ran into it numerous times, and that’s how I dealt with it.

    That’s how to deal with the IE issue.

    The deserialization should be amongst these lines. I’m posting the kind of arbitrary code I normally use:

    public static object DeserializeFromXML<T>(string _input)
        {
            object _temp = Activator.CreateInstance<T>();
            Type expected_type = _temp.GetType();
            _temp = null;
            XmlSerializer serializer = new XmlSerializer(expected_type);
            StringReader stringWriter = new StringReader(_input);
            object _copy = serializer.Deserialize(stringWriter);
            return _copy;
        }
    

    In the above example, I’m using templating for reusability sake. You should be able to call the method by saying DeserializeFromXML < Class1 >(_xml_input) where xml input is the string. That will force the compiler to use the definition of the given class to deserialize the XML input. That way you wouldn’t even have to change the encoding in the serialization. If it’s also a case where you may or may not know the data type to deserialize with, you can use a strategy design pattern where you register the root type of the XML with it’s associated generic type. Later on you can reference that registry and arbitrarily deserialize any XML input as long as the root type is registered. It’s a trick i normally use as well. If more is needed on this, let me know, and I’ll explain in detail.

    In addition,if you are running IE 9, the new update to IE 9 makes it difficult to view XML. Press F12 – go to developer tools and change your browser mode to run as IE 8 instead of IE 9.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a web service that accepts CAS proxy tickets over HTTP Basic authentication
I have a web service that requires special headers to be sent in the
I have a web service that stores data in a local SQL Server 2008
I have a web service that is running on IIS (6 or 7, doesn't
I have a web service that takes a byte[] and saves it. This works
I have a web service that serves widgets. It is hosted on a server
I have a web service that wish to call, and I want to pass
I have a web service that uses nhibernate, calls to the web service go
We have a web service that provides auto insurance quotes and a company that
I have a web service that I call via ajax that requires the user

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.