I am dying trying to import a large xml file with tons of special characters that keep throwing errors in my app. I get through about 76 records in the XML file before something causes a Token error. {"There was an error parsing the query. [ Token line number = 1,Token line offset = 725,Token in error = S ]"}
I have put my code below.
public void ReadXMLIntoDB()
{
OpenFileDialog ofd;
ofd = new OpenFileDialog();
ofd.Filter = "Xml Files|*.xml|AllFiles|*.*";
ofd.ShowDialog();
StreamReader sr = new StreamReader(ofd.FileName);
XmlTextReader xr = new XmlTextReader(sr);
int iNumRows = 0;
while (xr.ReadToFollowing("row") != false)
{
string sFacName, sFacID, sAddress, sCity, sState, sZip, sOwnerID, sOwnerName, sStreetNum, sStreetName, sStreetType, sPostDirectional, sPhone, sProgramCat, sProgramCatDes, sInspectionDate, sInspectionType, sInspectionDes, sSerialNum, sActionCode, sActionDes, sResultCode, sResultDes, sViolationCode, sViolationDes, sInspectionMemo;
xr.ReadToFollowing("value");
sFacName = xr.ReadInnerXml();
xr.ReadToFollowing("value");
sFacID = xr.ReadInnerXml();
xr.ReadToFollowing("value");
sAddress = xr.ReadInnerXml();
xr.ReadToFollowing("value");
sCity = xr.ReadInnerXml();
xr.ReadToFollowing("value");
sState = xr.ReadInnerXml();
xr.ReadToFollowing("value");
sZip = xr.ReadInnerXml();
xr.ReadToFollowing("value");
sOwnerID = xr.ReadInnerXml();
xr.ReadToFollowing("value");
sOwnerName = xr.ReadInnerXml();
xr.ReadToFollowing("value");
sStreetNum = xr.ReadInnerXml();
xr.ReadToFollowing("value");
sStreetName = xr.ReadInnerXml();
xr.ReadToFollowing("value");
sStreetType = xr.ReadInnerXml();
xr.ReadToFollowing("value");
sPostDirectional = xr.ReadInnerXml();
xr.ReadToFollowing("value");
sPhone = xr.ReadInnerXml();
xr.ReadToFollowing("value");
sProgramCat = xr.ReadInnerXml();
xr.ReadToFollowing("value");
sProgramCatDes = xr.ReadInnerXml();
xr.ReadToFollowing("value");
sInspectionDate = xr.ReadInnerXml();
xr.ReadToFollowing("value");
sInspectionType = xr.ReadInnerXml();
xr.ReadToFollowing("value");
sInspectionDes = xr.ReadInnerXml();
xr.ReadToFollowing("value");
sSerialNum = xr.ReadInnerXml();
xr.ReadToFollowing("value");
sActionCode = xr.ReadInnerXml();
xr.ReadToFollowing("value");
sActionDes = xr.ReadInnerXml();
xr.ReadToFollowing("value");
sResultCode = xr.ReadInnerXml();
xr.ReadToFollowing("value");
sResultDes = xr.ReadInnerXml();
xr.ReadToFollowing("value");
sViolationCode = xr.ReadInnerXml();
xr.ReadToFollowing("value");
sViolationDes = xr.ReadInnerXml();
xr.ReadToFollowing("value");
sInspectionMemo = xr.ReadInnerXml();
Regex.Replace(sInspectionMemo, @"[^\w\&#.@-]", "");
SqlCeConnection con;
SqlCeCommand cmd;
string cstr, sql;
cstr = @"Data Source=|DataDirectory|\foodDB.sdf";
sql = String.Format("Insert Into food(FacilityName,FacilityID,SiteAddress,City,State,ZipCode,OwnerID,OwnerName,StreetNumber,StreetName,StreetType,PostDirectional,Phone,ProgramCategory,ProgramCategoryDescription,InspectionDate,InspectionType,InspectionDescription,SerialNumber,ActionCode,ActionDescription,ResultCode,ResultDescription,ViolationCode,ViolationDescription,InspectionMemo) values ('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}','{12}','{13}','{14}','{15}','{16}','{17}','{18}','{19}','{20}','{21}','{22}','{23}','{24}','{25}')", sFacName, sFacID, sAddress, sCity, sState, sZip, sOwnerID, sOwnerName, sStreetNum, sStreetName, sStreetType, sPostDirectional, sPhone, sProgramCat, sProgramCatDes, sInspectionDate, sInspectionType, sInspectionDes, sSerialNum, sActionCode, sActionDes, sResultCode, sResultDes, sViolationCode, sViolationDes, sInspectionMemo);
con = new SqlCeConnection(cstr);
cmd = new SqlCeCommand(sql, con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
++iNumRows;
Action ac = delegate()
{
txrows.Text = iNumRows.ToString();
};
Dispatcher.BeginInvoke(ac);
}
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
Thread t = new Thread(ReadXMLIntoDB);
t.Start();
}
From what I can tell, the only obvious thing I can see in that record that might cause an error is a 's where a ' should be. Can someone please help me?