I am trying to build a helper library that we will use to construct the ImportEngine XML files for a given PDF, but each time I try to run ImportEngine.BeginProcess(myStream) I get "Root element is missing."
Here is what I have been doing:
public void CreatePdf(string xmlTemplatePath, sting pdfName, string xmlTemplatePath){
var xmlTemplate = XDocument.Load(xmlTemplatePath);
var documentSettingsElement = xmlTemplate.Root.Descendants().FirstOrDefault(e => e.Name.LocalName.ToLower() == "document");
documentSettingsElement?.SetAttributeValue("name", pdfName);
var fileSettingsElement = xmlTemplate.Root.Descendants().FirstOrDefault(e => e.Name.LocalName.ToLower() == "fileref");
fileSettingsElement?.SetAttributeValue("ref", pdfPath);
var importDoc = ParseXmlTemplate(pdfName, pdfPath, xmlTemplatePath);
var writerSettings = new XmlWriterSettings
{
OmitXmlDeclaration = false,
Indent = false,
IndentChars = "",
ConformanceLevel = ConformanceLevel.Document,
NewLineChars = "",
CheckCharacters = true,
Encoding = new UTF8Encoding(false)
};
var memoryStream = new MemoryStream();
using (var writer = XmlWriter.Create(memoryStream, writerSettings))
{
importDoc.WriteTo(writer);
}
memoryStream.Position = 0;
using (var session = CreateSession(_laserficheServer, _laserficheRepository, _laserficheUsername, _laserfichePassword))
{
var lfImportEngine = CreateImportEngine(_laserficheUploadPath, _laserficheVolume, session);
var import = lfImportEngine.BeginProcess(memoryStream);
while (!import.IsCompleted)
{
// wait for 1 second, and then refresh the status
Thread.Sleep(1000);
import.Refresh();
}
if (import.HasFailed)
{
session.LogOut();
throw new Exception(import.FailureReason.Message);
}
}
}
Here is the XML from my template file:
<?xml version="1.0" encoding="utf-8"?>
<LF:importengine xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://laserfiche.com/namespaces/importengine ImportEngine.xsd"
xmlns:LF="http://laserfiche.com/namespaces/importengine" version="1.0">
<LF:toc on_document_conflict="unique" on_folder_conflict="unique">
<LF:document name="DefaultDocumentName" comment="" language="EN" template="">
<LF:electronic_document content_type="application/pdf" extension="pdf">
<LF:fileref ref="C:\Users\visual studio 2015\Projects\LaserficheProofOfConcept\Files\LaserficheSdkImport.pdf" />
</LF:electronic_document>
</LF:document>
</LF:toc>
</LF:importengine>
I'm hoping I'm just missing something simple, but have spent the better part of two days trying to figure out why this doesn't work.
I have omitted a couple methods that I use to build the Session object and the ImportEngine. I have verified that if I use the String overload for BeginProcess and supply a path to the XML template file, ImportEngine performs the import without issue.