//Give me an example for Serialising Multidimensional array in c sharp.

//While searching in net i found this code snippet

[XmlIgnore]
public char[,] Data;

[XmlElement("Data")]
[EditorBrowsable(EditorBrowsableState.Never)]
public char[][] XmlData
{
  get { /* copy contents of Data into a jagged array */ }
  set { /* copy contents of jagged array into Data */ }
}

//Please explain this in detail.

Give me an example for Serialising Multidimensional array in c sharp.

That's an easy one: you can't do it. Multidimensional array serialization to XML isn't supported by .NET. You'll need to use a different type such as an array of arrays or nested List<>. But once you do that, it's trivial:

public class Program
{
    public static void Main()
    {
        try
        {
            var foo = new Foo(new char[,] {
                {'a','b','c'},
                {'1','2','3'}
            });

            Console.WriteLine(Foo.Serialize(foo));
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
        }
    }
}

public class Foo
{
    public char[][] Data;

    public Foo()
    {
    }

    public Foo(char[,] init)
    {
        int cols = init.GetLength(1);

        Data = new char[init.Rank][];

        for (int i = 0; i < init.Rank; i++)
        {
            Data[i] = new char[cols];

            for (int j = 0; j < cols; j++)
            {
                Data[i][j] = init[i, j];
            }
        }
    }

    public static Foo Deserialize(string xml)
    {
        return XmlWrapper.Deserialize<Foo>(xml);
    }

    public static string Serialize(Foo obj)
    {
        return XmlWrapper.Serialize(obj);
    }
}

This code uses a helper class for doing the serialization because the code is pretty much just boilerplate, and there's not much point duplicating it all over the place except when you have special needs. Here's the helper class (comments removed for brevity):

public static class XmlWrapper
{
    public static T Deserialize<T>(string xml)
    {
        using (var stream = new StringReader(xml))
        {
            var xs = new XmlSerializer(typeof(T));

            return (T)xs.Deserialize(stream);
        }
    }

    public static string Serialize<T>(T obj)
    {
        return Serialize<T>(obj, string.Empty, string.Empty);
    }

    public static string Serialize<T>(T obj, string nsPrefix, string ns)
    {
        using (var ms = new MemoryStream())
        {
            using (var sw = new StreamWriter(ms, Encoding.UTF8))
            {
                var xs = new XmlSerializer(typeof(T));
                var xmlns = new XmlSerializerNamespaces();

                xmlns.Add(nsPrefix, ns);
                xs.Serialize(sw, obj, xmlns);

                return Encoding.UTF8.GetString(ms.ToArray()).Trim();
            }
        }
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.