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 7983765
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T11:03:44+00:00 2026-06-04T11:03:44+00:00

I have been using some code to create MTOM by using code from MSDN

  • 0

I have been using some code to create MTOM by using code from MSDN.

It seems that there is an error and I cannot understand where the problem lies as one of the users on the forum pointed out that there is an error.

The file (JPEG) data get corrupted after a de-serialization. The complete code is listed below.

 public class Post_7cb0ff86_5fe1_4266_afac_bcb91eaca5ec
            {
                [DataContract()]
                public partial class TestAttachment
                {
                    private byte[] fileField;
                    private string filenameField;

                    [DataMember()]
                    public byte[] File
                    {
                        get
                        {
                            return this.fileField;
                        }
                        set
                        {
                            this.fileField = value;
                        }
                    }
                    [DataMember()]
                    public string Filename
                    {
                        get
                        {
                            return this.filenameField;
                        }
                        set
                        {
                            this.filenameField = value;
                        }
                    }
                }
                public static void Test()
                {
                    string Filename = "Image.jpg";
                byte[] file = File.ReadAllBytes(Filename);

                TestAttachment Attachment = new TestAttachment();
                Attachment.Filename = Filename;
                Attachment.File = file;
                MemoryStream MTOMInMemory = new MemoryStream();
                XmlDictionaryWriter TW = XmlDictionaryWriter.CreateMtomWriter(MTOMInMemory, Encoding.UTF8, Int32.MaxValue, "");
                DataContractSerializer DCS = new DataContractSerializer(Attachment.GetType());
                DCS.WriteObject(TW, Attachment);
                TW.Flush();
                Console.WriteLine(Encoding.UTF8.GetString(MTOMInMemory.ToArray()));
                var v = DeserializeMTOMMessage(Encoding.UTF8.GetString(MTOMInMemory.ToArray()));
                File.WriteAllBytes(v.Filename,v.File);
                }

                public static TestAttachment DeserializeMTOMMessage(string MTOMMessage)
                {

                    try
                    {

                        MemoryStream MTOMMessageInMemory = new MemoryStream(UTF8Encoding.UTF8.GetBytes(MTOMMessage));

                        XmlDictionaryReader TR = XmlDictionaryReader.CreateMtomReader(MTOMMessageInMemory, Encoding.UTF8, XmlDictionaryReaderQuotas.Max);

                        DataContractSerializer DCS = new DataContractSerializer(typeof(TestAttachment));

                        return (TestAttachment)DCS.ReadObject(TR);

                    }
                    catch
                    {

                        return null;

                    }

                }
            }

I would be grateful if someone can help me in pointing out where the problem is. I am new to XOP/MTOM and find it hard to track down where the error might be. Either serialization or de-serialization.

Thank you

  • 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-04T11:03:46+00:00Added an answer on June 4, 2026 at 11:03 am

    There’s a bug in your code.
    Change your method call

    MTOMInMemory.Position = 0;
    DeserializeMTOMMessage(Encoding.UTF8.GetString(MTOMInMemory.ToArray()));
    

    to

    DeserializeMTOMMessage(MTOMInMemory.ToArray())
    

    and the implementation to

     public static TestAttachment DeserializeMTOMMessage(byte[] MTOMMessage) 
        { 
            try 
            { 
                MemoryStream MTOMMessageInMemory = new MemoryStream(MTOMMessage);
                XmlDictionaryReader TR = XmlDictionaryReader.CreateMtomReader(MTOMMessageInMemory, Encoding.UTF8, XmlDictionaryReaderQuotas.Max); 
                DataContractSerializer DCS = new DataContractSerializer(typeof(TestAttachment)); 
                return (TestAttachment)DCS.ReadObject(TR); 
            } 
            catch
            {
    
                return null;
            } 
        } 
    

    what you’ve done was some double conversion from utf8 to byte array and vice versa, which ended up creating not the original byte array you were using

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

Sidebar

Related Questions

I have been given some matlab code compiled using the .net compiler. I can
Some of mysql code I have been looking using utf8_slovenian_ci in it, why is
I have been using Java's ConcurrentMap for a map that can be used from
I have been using Perl for some time, but today I came across this
I have been using InstallJammer for some time, but it's development is now discontinued.
I've been using Mercurial for some development lately and have been loving it. I'm
I have been using the AddressBook api of the iPhone for some time now.
Have been looking on some tutorials for drawing canvas using SurfaceView, but the only
I have been doing some research for using MSMQ. Following 2 gave me fundamental
I've been using Firebug to debug some javascript I have on one of my

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.