Quantcast
Channel: NetOffice - MS Office in .NET
Viewing all articles
Browse latest Browse all 1741

Commented Unassigned: Excel XmlImportXml does not work [20704]

$
0
0
I have a function that imports xml data into a range in Excel. This have been working fine using the Excel interop, but when I changed to NetOffice it didn't work anymore.

I've tried to use the function differently, but no luck so far....
Sample:
strXML contains the XML to be inserted

```
Dim excel As NetOffice.ExcelApi.Application = Nothing
Dim exWb As NetOffice.ExcelApi.Workbook
Dim sht As NetOffice.ExcelApi.Worksheet

excel = New NetOffice.ExcelApi.Application
Dim xsd As NetOffice.ExcelApi.XmlMap = Nothing
exWb.XmlImportXml(strXML, xsd, True, sht.Range("A1"))

```

Error:
Parameter not optional. (Exception from HRESULT: 0x8002000F (DISP_E_PARAMNOTOPTIONAL))

All parameters has been filled. The xsd parameter is an out parameter...
I've tried to reduce to (strXML, xsd) parameters. But same result.

I've also tried to go directly from a dataset with dataset.GetXML and dataset.GetXmlSchema
Same result.
Comments: Using Excel 2010 and NetOffice for .NET 4.5 obtained as a NuGet package ``` using System; using Excel = NetOffice.ExcelApi; using System.IO; namespace NetOfficeBugs { class Program { private static string _schema = "<xs:schema attributeFormDefault=\"unqualified\" elementFormDefault=\"qualified\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">\r\n <xs:element name=\"ArrayOfCommentInfo\">\r\n <xs:complexType>\r\n <xs:sequence>\r\n <xs:element name=\"CommentInfo\" maxOccurs=\"unbounded\" minOccurs=\"0\">\r\n <xs:complexType>\r\n <xs:sequence>\r\n <xs:element type=\"xs:byte\" name=\"CommentId\"/>\r\n <xs:element type=\"xs:short\" name=\"NodeId\"/>\r\n <xs:element type=\"xs:string\" name=\"TicketTitle\"/>\r\n <xs:element type=\"xs:string\" name=\"DateFormatted\"/>\r\n <xs:element type=\"xs:string\" name=\"Complaint\"/>\r\n </xs:sequence>\r\n </xs:complexType>\r\n </xs:element>\r\n </xs:sequence>\r\n </xs:complexType>\r\n </xs:element>\r\n</xs:schema>"; static void Main(string[] args) { using (var exApp = new Excel.Application()) { Console.WriteLine($"Excel Version: {exApp.Version}"); var xml = File.ReadAllText("example.xml"); exApp.Visible = true; var workbook = exApp.Workbooks.Add(); var sheet = (Excel.Worksheet)workbook.Worksheets[1]; //Uncomment what you want to test //ImportXmlUnderlyingObject(xml, workbook, sheet); //ImportXmlNetOffice(xml, workbook, sheet); } Console.WriteLine(); Console.WriteLine("Done executing!"); Console.ReadLine(); } private static void ImportXmlNetOffice(string xml, Excel.Workbook workbook, Excel.Worksheet sheet) { //This code works if you use the standard Excel libraries from Microsoft var destination = sheet.Cells[1, 1]; var map = workbook.XmlMaps.Add(_schema); workbook.XmlImportXml(xml, out map, true, destination); //But causes this exception in NetOffice: /* System.Runtime.InteropServices.COMException was unhandled ErrorCode=-2147467259 HResult=-2147467259 Message=See inner exception(s) for details. Source=NetOffice StackTrace: bij NetOffice.Invoker.MethodReturn(COMObject comObject, String name, Object[] paramsArray) bij NetOffice.ExcelApi._Workbook.XmlImportXml(String data, XmlMap& importMap, Object overwrite, Object destination) bij NetOfficeBugs.Program.ImportXmlNetOffice(String xml, Workbook workbook, Worksheet sheet) in C:\Users\johanb\Source\Repos\TicketSystemTools\NetOfficeBugs\Program.cs:regel 47 bij NetOfficeBugs.Program.Main(String[] args) in C:\Users\johanb\Source\Repos\TicketSystemTools\NetOfficeBugs\Program.cs:regel 31 bij System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) bij System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) bij Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() bij System.Threading.ThreadHelper.ThreadStart_Context(Object state) bij System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) bij System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) bij System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) bij System.Threading.ThreadHelper.ThreadStart() InnerException: HResult=-2146232828 Message=Het doel van een aanroep heeft een uitzondering veroorzaakt. Source=mscorlib StackTrace: bij System.RuntimeType.InvokeDispMethod(String name, BindingFlags invokeAttr, Object target, Object[] args, Boolean[] byrefModifiers, Int32 culture, String[] namedParameters) bij System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object[] providedArgs, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParams) bij NetOffice.Invoker.MethodReturn(COMObject comObject, String name, Object[] paramsArray) InnerException: ErrorCode=-2147352561 HResult=-2147352561 Message=Parameter niet optioneel. (Uitzondering van HRESULT: 0x8002000F (DISP_E_PARAMNOTOPTIONAL)) InnerException: */ } private static void ImportXmlUnderlyingObject(string xml, Excel.Workbook workbook, Excel.Worksheet sheet) { var destination = sheet.Cells[1, 1]; var map = workbook.XmlMaps.Add(_schema).UnderlyingObject; dynamic dynamicWorkbook = workbook.UnderlyingObject; dynamicWorkbook.XmlImportXml(xml, out map, true, destination.UnderlyingObject); } } } ```

Viewing all articles
Browse latest Browse all 1741

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>