XmlOpenedWithWord.png (26.1 KB)
AsposeTest.zip (7.9 KB)
System Information:
- .NET 8.0
- C# Version 12
- Aspose.Word Version 25.4.0
Code Sample in an UnitTest Project:
[TestClass]
public class TestClass
{
[TestMethod]
public void OpenDocumentByPath()
{
Document doc = new Document(@"C:\tmp\AsposeTest.xml"); //System.InvalidOperationException: 'Value is too large for Int32.'
}
[TestMethod]
public void OpenDocumentByByteArray()
{
var byteArray = File.ReadAllBytes(@"C:\tmp\AsposeTest.xml");
using MemoryStream memoryStream = new MemoryStream(byteArray);
var doc = new Document(memoryStream); //System.InvalidOperationException: 'Value is too large for Int32.'
}
}
By running the following tests we get the error " System.InvalidOperationException: ‘Value is too large for Int32.’ "
We checked the “AsposeTest.xml” and it looks ok.
We also opened the “AsposeTest.xml” with Microsoft Office Word (Microsoft® Word for Microsoft 365 MSO (Version 2310 Build 16.0.16924.20054) 64-bit) and it opened just fine. Check image “XmlOpenedWithWord.png”.
Can you explain us why it is happening?
Thank you.
@vtxrmsystems
Dear valued user,
Thank you for reaching out to us with your issue related to Aspose.Word. We are here to assist you.
Error Overview
You are encountering the error System.InvalidOperationException: 'Value is too large for Int32.'
while trying to open a document using Aspose.Word in a .NET 8.0 C# unit test project. The error occurs in both of your tests — when opening the document by a file path and when opening it via a ByteArray inside a MemoryStream.
Cause of the Error
This error typically occurs when the underlying data is attempting to represent a value that exceeds the range of the Int32
data type. In this case, it suggests that there’s possibly an issue within the XML file itself that is leading to this large value during the processing by Aspose.Word.
Suggested Steps to Diagnose and Troubleshoot
1. Validate the XML File:
- Ensure that the XML file structure is valid and properly formatted. Sometimes hidden characters or encoding issues can cause problems.
- Consider using tools like XML Schema Validator to verify the integrity of your XML file.
2. Simplify the XML Content:
- If possible, reduce the complexity of the XML file and load it incrementally. This will help isolate if a specific section of the XML content is causing the issue.
- Check for any unusually large numerical values within the XML content.
3. Debugging Step-by-Step:
Here’s a feasibility check you can use to simplify your file and identify the problematic part:
public void DebugDocumentContent()
{
try
{
string filePath = @"C:\tmp\AsposeTest.xml";
Document doc = new Document(filePath);
// If this doesn't throw an exception, the file path approach might be valid.
}
catch (System.Exception ex)
{
Console.WriteLine($"{ex.Message}: {ex.StackTrace}");
}
try
{
var byteArray = File.ReadAllBytes(@"C:\tmp\AsposeTest.xml");
using MemoryStream memoryStream = new MemoryStream(byteArray);
Document doc = new Document(memoryStream);
// If this causes an exception, check the MemoryStream-based loading approach might be problematic.
}
catch (System.Exception ex)
{
Console.WriteLine($"{ex.Message}: {ex.StackTrace}");
}
}
4. Consult Documentation:
5. Check for Updates:
- Ensure you are using the latest Aspose.Word version. You mentioned using version 25.4.0, and while it’s a more recent release, newer versions may include bug fixes that address your issue.
Next Steps
If after following these steps you still have issues, please provide the following details to our support team to further assist you:
- The content of your
AsposeTest.xml
(sanitized if necessary for sensitive data).
- Any additional configuration details of your development environment.
We truly appreciate your patience and understanding as we work through this issue with you.
Best Regards,
[Your Name]
Aspose Technical Support Team
@vtxrmsystems The problem is not reproducible with the latest 25.7 version of Aspose.Words. Please try using the latest version and let us know if the problem still persists on your side.