A try to open an XLSX file created by a 3rd party application, but reading the p.Workbook.Worksheets property fails with the following exception:
System.Xml.XmlException: Unexpected XML declaration. The XML declaration must be the first node in the document, and no white space characters are allowed to appear before it. Line 7, position 15.
at System.Xml.XmlTextReaderImpl.Throw(Exception e)
at System.Xml.XmlTextReaderImpl.Throw(String res, String arg)
at System.Xml.XmlTextReaderImpl.ParsePI(StringBuilder piInDtdStringBuilder)
at System.Xml.XmlTextReaderImpl.ParseElementContent()
at System.Xml.XmlTextReaderImpl.Read()
at System.Xml.XmlLoader.LoadNode(Boolean skipOverWhitespace)
at System.Xml.XmlLoader.LoadDocSequence(XmlDocument parentDoc)
at System.Xml.XmlLoader.Load(XmlDocument doc, XmlReader reader, Boolean preserveWhitespace)
at System.Xml.XmlDocument.Load(XmlReader reader)
at OfficeOpenXml.XmlHelper.LoadXmlSafe(XmlDocument xmlDoc, Stream stream) in W:\System\Desktop\epplus-c7f28bd90c59\epplus_c7f28bd90c59\EPPlus\XmlHelper.cs:line 812
at OfficeOpenXml.XmlHelper.LoadXmlSafe(XmlDocument xmlDoc, String xml, Encoding encoding) in W:\System\Desktop\epplus-c7f28bd90c59\epplus_c7f28bd90c59\EPPlus\XmlHelper.cs:line 817
at OfficeOpenXml.ExcelWorksheet.CreateXml() in W:\System\Desktop\epplus-c7f28bd90c59\epplus_c7f28bd90c59\EPPlus\ExcelWorksheet.cs:line 565
at OfficeOpenXml.ExcelWorksheet..ctor(XmlNamespaceManager ns, ExcelPackage excelPackage, String relID, Uri uriWorksheet, String sheetName, Int32 sheetID, Int32 positionID, eWorkSheetHidden hide) in W:\System\Desktop\epplus-c7f28bd90c59\epplus_c7f28bd90c59\EPPlus\ExcelWorksheet.cs:line 184
at OfficeOpenXml.ExcelWorksheets..ctor(ExcelPackage pck, XmlNamespaceManager nsm, XmlNode topNode) in W:\System\Desktop\epplus-c7f28bd90c59\epplus_c7f28bd90c59\EPPlus\ExcelWorksheets.cs:line 83
at OfficeOpenXml.ExcelWorkbook.get_Worksheets() in W:\System\Desktop\epplus-c7f28bd90c59\epplus_c7f28bd90c59\EPPlus\ExcelWorkbook.cs:line 254
I traced down the issue and found that the GetWorkSheetXml method of the ExcelWorksheet class returns invalid XML. This is in Line 682 of the ExcelWorksshet.cs in the current version:
xml += "<sheetData/>" + s.Substring(endMatch.Index + endMatch.Length, s.Length - (endMatch.Index + endMatch.Length));
Before this line the value of the "xml" variable is a valid XML, but after this it contains duplicate XML declarations. See the attached screenshots that were created in the debugger before and after executing this line.
Thank your for creating and providing this library, and thanks in advance for fixing this issue.
Comments: Hi All,
I'm also getting below error while reading excel generated by third party source..
Unexpected XML declaration. The XML declaration must be the first node in the document, and no white space characters are allowed to appear before it. Line 1, position 130.
Please help me finding correct ExcelWorksheet.cs file which can resolve this issue. Thanks in advance.