Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Mon Jul 15, 2024 9:51 am

hi,
i'm using this method,
Code: Select all
document.LoadFromStream(stream, Spire.Doc.FileFormat.Docx);


in this case working fine:

Code: Select all
MemoryStream stream = new MemoryStream(value);       
Spire.Doc.Document document = new Spire.Doc.Document();
document.LoadFromStream(stream, Spire.Doc.FileFormat.Docx);


but if i try this, exception System.Xml.XmlException: ''':
Code: Select all
string s = System.Text.Encoding.Unicode.GetString(value, 0, (value).Length);
byte[] ba = Encoding.Unicode.GetBytes(s);
MemoryStream stream = new MemoryStream(ba);       
Spire.Doc.Document document = new Spire.Doc.Document();
document.LoadFromStream(stream, Spire.Doc.FileFormat.Docx);


i checked the leght of both byte array and they are sames.
what i wrong?

emamanu_1987
 
Posts: 14
Joined: Fri Jun 21, 2024 10:23 am

Tue Jul 16, 2024 3:11 am

Hello,

Thanks for your inquiry.
Kindly note that the byte array after Unicode encoding may be different from the byte array obtained by directly reading the file. To compare whether the two byte arrays are completely consistent, you can verify it through the following code. In any case, we do not recommend customers to load files after encoding. If you have other questions, please feel free to write back.
Code: Select all
byte[] streamData = File.ReadAllBytes("in.docx");
// Convert the byte array to a Unicode-encoded string
string s = System.Text.Encoding.Unicode.GetString(streamData, 0, streamData.Length);
// Convert the string back to a Unicode-encoded byte array
byte[] stream1Data = Encoding.Unicode.GetBytes(s);
if (!streamData.SequenceEqual(stream1Data))
{
    Console.WriteLine("The two MemoryStream objects contain different data.");

    // Find the first different byte position
    int firstDifferentIndex = -1;
    for (int i = 0; i < Math.Min(streamData.Length, stream1Data.Length); i++)
    {
        if (streamData[i] != stream1Data[i])
        {
            firstDifferentIndex = i;
            break;
        }
    }
    if (firstDifferentIndex != -1)
    {
        Console.WriteLine($"The first different byte is at index {firstDifferentIndex}:");
        Console.WriteLine($"stream data: 0x{streamData[firstDifferentIndex]:X2}");
        Console.WriteLine($"stream1 data: 0x{stream1Data[firstDifferentIndex]:X2}");
    }
    else
    {
        // If the lengths are different, print the length difference
        Console.WriteLine($"The two MemoryStream objects have different lengths. " +
                          $"stream length: {streamData.Length}, stream1 length: {stream1Data.Length}");
    }
}
else
{
    Console.WriteLine("The two MemoryStream objects contain the same data.");
}

Sincerely,
William
E-iceblue support team
User avatar

William.Zhang
 
Posts: 419
Joined: Mon Dec 27, 2021 2:23 am

Tue Jul 16, 2024 6:11 am

thank you.
i solved using base64 conversion.

emamanu_1987
 
Posts: 14
Joined: Fri Jun 21, 2024 10:23 am

Tue Jul 16, 2024 9:38 am

Hello,

Thanks for your feedback.
If you have any questions about our products in the future, please feel free to write back.

Sincerely,
William
E-iceblue support team
User avatar

William.Zhang
 
Posts: 419
Joined: Mon Dec 27, 2021 2:23 am

Return to Spire.Doc