我很想导入一个包含大量特殊字符的大型 xml 文件,这些特殊字符会在我的应用程序中不断抛出错误。在导致令牌错误之前,我通过 XML 文件中的大约 76 条记录。{"解析查询时出错。[令牌行号=1,令牌行偏移量=725,错误令牌=S]"}
我把我的代码放在下面。
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();
}
据我所知,我可以在该记录中看到的唯一可能导致错误的明显内容是 a ' 应该在哪里。有人可以帮帮我吗?