Basically, I'm trying to open an excel file and read the its xml. However when I run my program I get a run time saying "Root element is missing". I checked a number of solutions and none of them fix my issue. I've also checked that my file exists and can be accessed. Not sure what the issue is here if anyone could direct my in the right direction it would be great. Also I'm not sure if I should be using FileStream
here.
My code is as follows:
string path = @"C:\Users\Craig\Documents\newTest.xlsx";
using (FileStream xml = File.Open(path, FileMode.Open, FileAccess.Read))
{
// Encode the XML string in a UTF-8 byte array
byte[] encodedString = Encoding.UTF8.GetBytes(xmlDoc.InnerXml);
// Put the byte array into a stream and rewind it to the beginning
MemoryStream ms = new MemoryStream(encodedString);
ms.Flush();
ms.Position = 0;
// Build the XmlDocument from the MemorySteam of UTF-8 encoded bytes
if (xml.Position > 0)
{
xml.Position = 0;
}
XDocument xDoc = XDocument.Load(ms);
Console.WriteLine(xml);
}