0

I am trying to convert Object to xml using silverlight 5 and c# and i have following error:

Error   1   'System.Text.Encoding.GetString(byte[])' is inaccessible due to its protection level    

corresponding to line:

 return utf8.GetString(mem.ToArray());

in my Xml.cs class

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Xml.Serialization;
using System.IO;
using System.Text;

namespace SliderLastTry
{
    public static class Xml
    {
        public static string ToXml(this object objectToSerialize)
        {
            var mem = new MemoryStream();
            var ser = new XmlSerializer(objectToSerialize.GetType());
            ser.Serialize(mem, objectToSerialize);
            var utf8 = new UTF8Encoding();
            return utf8.GetString(mem.ToArray());
        }
    }
}

Paramter.cs is:

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace SliderLastTry
{
    public  class Parameter 
    { 
        public  string Name {get; set; } 
    }  
}

Main function containing class is:

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace SliderLastTry
{
    public static class ControlClass
    {
        public static void Main()
        {
            Parameter pram = new Parameter();
            pram.ToXml();

        }

    }
}

Could some one please help me in fixing my error ?

Sss
  • 1,519
  • 8
  • 37
  • 67

2 Answers2

4

I guess silverlight doesn't have the overload which takes only byte[] for some reason.

You just need to use another overload which takes index and count as well.

var bytes = mem.ToArray();
return utf8.GetString(bytes, 0, bytes.Length);

FWIW GetString internally calls another overload the same way :)

Sriram Sakthivel
  • 72,067
  • 7
  • 111
  • 189
2

To be honest, your best bet here is actually to use StringWriter, since a .NET string is not a direct map to UTF-8 (it is actually UTF-16, if anything):

using(var writer = new StringWriter())
{
    ser.Serialize(writer, objectToSerialize);
    return writer.ToString();
}

This also has the performance advantage of avoiding an additional duplicate of all the data (string vs byte[]).

If you must use the byte[] version, you can use a different overload to specify the bounds of the array. Note also that passing in the underlying buffer avoids a third duplicate of the data (i.e. the underlying byte[] in the memory-stream, the temporary byte[] returned from ToArray(), and the data that ends up in the string).

return utf8.GetString(mem.GetBuffer(), 0, (int)mem.Length);
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • thanks a lot. You really xml expert. I have one mre question suppose if i print this xml in a file. Then is it possible to print it in formatted manner(rather then a continous string) ? – Sss May 20 '14 at 11:32
  • 1
    @user234839 that is controlled by the `XmlWriterSettings` class; to do *that* you need to have an `XmlWriter` as well; see here, for example: http://stackoverflow.com/q/5414617/23354 - basically, you'd have a `StringWriter` that is wrapped by the `XmlWriter`, then have the `XmlSerializer` write to the `XmlWriter`; see MSDN: http://msdn.microsoft.com/en-us/library/system.xml.xmlwritersettings(v=vs.95).aspx – Marc Gravell May 20 '14 at 11:48