0

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;
jazb
  • 5,498
  • 6
  • 37
  • 44
  • 1
    Check with a hex editor if there is a BOM([Byte-Order-Mark](https://en.wikipedia.org/wiki/Byte_order_mark)) at the beginning of the file. Maybe such thing is created, and maybe it confuses the reader. – zx485 Jan 28 '22 at 03:16
  • Thanks so much. I've checked using ultra edit hex mode. I'm still about confused on why this wouldn't be removed when I am replacing the entire xml declaration using the above codes? – IndianaCones88 Jan 28 '22 at 04:59
  • `StreamWriter` lets you have the option to specify the encoding. Experiment to find the parameters working for your situation. – zx485 Jan 28 '22 at 05:14

0 Answers0