I'm having some trouble with xml declaration and encoding. I have some excel files which have an issue with the encoding/declaration. I have no control over how the files are created. They are received directly from a client
I'm trying to using C# to rewrite the declaration and insert a the first node. However I keep running into the same response. I understand the error message is clear, what's not clear to me is why the newly created declaration is not solving this issue.
I assumed because it reads as the first child in the node which I checked and by prepending it would solve my issue but it hasn't. I've been stuck on this for quite a while. Any push in the right direction would greatly appreciated.
Error occurs on the last line --> var worksheets = xlPack.Workbook.Worksheets;
Actual error message -->
Unexpected XML Declaration. XML Declaration must be the first node in the document and no whitespace characters are allowed to appear before it line 2 position 1228.
sample xml
<?xml version="1.0" encoding="utf-8" ?>
<p:properties xmlns:p=http://schemas.microsoft.com/office/2006/metadata/properties xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance xmlns:pc=http://schemas.microsoft.com/office/infopath/2007/PartnerControls>
<documentManagement />
</p:properties>
using (StreamWriter sw = new StreamWriter(filePath + exportFileName))
{
using (ExcelPackage xlPack = new ExcelPackage(new FileInfo(importFile)))
{
var xmlDec = xlPack.Workbook.WorkbookXml.CreateXmlDeclaration("1.0", null, null);
var root = xlPack.Workbook.WorkbookXml.DocumentElement;
var roottwo = xlPack.Workbook.WorkbookXml;
var originalXMLDec = roottwo.FirstChild;
var originalXMLDecread = roottwo.FirstChild.Value;
var firstChildCheck = roottwo.FirstChild.NodeType;
var checkNodeType = xmlDec.NodeType;
roottwo.RemoveChild(originalXMLDec);
roottwo.PrependChild(xmlDec);
var rootTwo = root.InnerXml;
var worksheets = xlPack.Workbook.Worksheets;