automationFormation
automationFormation

Reputation: 31

Memory Stream with 2d array

I'm using this code to copy data from a 2d object array into the memory stream. The memory stream shows the count of the data that has been read from the 2d array. But in the end I'm getting an empty string. I can't understand why. However, using the soapformatter, the string is not empty but it's enclosed in soap format so that isn't useful. Here's the code with BinaryFormatter.

        Worksheet sheet = (Worksheet)workBookIn.Sheets[1];
        Range excelRange = sheet.UsedRange;
        object[,] valueArray = (object[,])excelRange.get_Value(
            XlRangeValueDataType.xlRangeValueDefault);
        using (var ms = new MemoryStream())
        {
            BinaryFormatter formatter = new BinaryFormatter();
            //valueArray = 2d object array
            formatter.Serialize(ms, valueArray);
            ms.Position = 0;
            return Encoding.UTF8.GetString(ms.ToArray());
        }

Upvotes: 2

Views: 655

Answers (1)

Deepak Bhatia
Deepak Bhatia

Reputation: 1100

I think you must be using XmlSerializer as follows:

    Worksheet sheet = (Worksheet)workBookIn.Sheets[1];
    Range excelRange = sheet.UsedRange;
    object[,] valueArray = (object[,])excelRange.get_Value(
        XlRangeValueDataType.xlRangeValueDefault);

    XmlSerializer xs = new XmlSerializer(valueArray.GetType());

    using(StringWriter sw = new StringWriter())
    using(XmlWriter writer = XmlWriter.Create(sw))
    {
         xs.Serialize(writer, valueArray);
         var xml = sw.ToString(); // Your XML
    }

Upvotes: 0

Related Questions