Sss
Sss

Reputation: 1529

c# How to convert Object to XML

I am c# and silverlight 5 beginner. I have to clear one omportanyt doubt with senior developers in silverlight.

I have an object in c# and i have to obtain xml corresponding to that object.

Could some one explain through an example how to do that(any simple example of class and it's object and then xml obtained)

I have read on google some document and they discuss some method call serialize() capable of doing so. Am i right ? But how it do so could some one please explain throught c# class and it's object and xml obtained ?

Would be a big help. EDIT: My try 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;
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());
        }
    }
}

The next 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  class Parameter 
    { 
        public  string Name {get; set; } 
    }  
}

The 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();

        }

    }
}

And the error obtained is:

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

CORRESPONDING TO THE LINE/

return utf8.GetString(mem.ToArray()); in Xml class. How toma the solution of it ?

Upvotes: 1

Views: 7099

Answers (2)

Marco
Marco

Reputation: 57593

I use this class

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.GetBuffer(), 0, (int)mem.Length);
    }
}

With this extension method you can do

var xml = your_object.ToXml();

Upvotes: 1

Marc Gravell
Marc Gravell

Reputation: 1063358

var writer = new StringWriter();
var serializer = new XmlSerializer(typeof(YourData));
serializer.Serialize(writer, obj);
string xml = writer.ToString();

See also MSDN

Upvotes: 2

Related Questions