Ed Tshuma

Ed Tshuma

  • 1.4k
  • 249
  • 996

How do i resolve incorrect syntax error when parsing an XML

Aug 28 2019 5:22 AM
I have a program to import data from an XML file into a SQL Server table. I am using the project by @Altafi Ansari here https://www.c-sharpcorner.com/article/xml-file-to-sql-database/. When i debug the program i get an "Incorrect syntax near 'TRANSACTION'" error as the flow starts creating the table . My observation are that the program can interpret the EXPORT_HEADER tag but the TRANSACTION tag is not being interpreted. I have attached my XML file here for clarity.
 
My XML is structured as below :
  1. <?xml version="1.0" encoding="utf-16"?>   
  2. <EXPORT_HEADER xmlns="http://www.sir.com/SFI/Export/GL_Export/20051005" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sir.com/SFI/Export/GL_Export/20051005 GL_EXPORT.xsd" total_transactions="4616" total_accnts="2000">   
  3. <TRANSACTION business_type_code="DIRECT" term="NB" />   
  4. <TRANSACTION business_type_code="DIRECT" term="NB" />   
  5. </EXPORT_HEADER>
However when i use the TRANSACTION tag section which contains the actual data i want to commit im getting the syntax error above: 
  1. DataSet DS = new DataSet();     
  2. DS.ReadXml(XMlFile);     
  3. DataTable dt = DS.Tables[1];     
  4. if (dt.Columns.Count == 0)     
  5. dt.ReadXmlSchema(XMlFile);     
  6. dt.ReadXml(XMlFile);   
As a check i tried using EXPORT_HEADER section and the data in the header is successfully committed and table also created:
  1. DataSet DS = new DataSet();   
  2. DS.ReadXml(XMlFile);   
  3. DataTable dt = DS.Tables[0];   
  4. if (dt.Columns.Count == 0)   
  5. dt.ReadXmlSchema(XMlFile);   
  6. dt.ReadXml(XMlFile);

Answers (1)