我正在试图导入一个包含大量特殊字符的大型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();
}
据我所知,我在该记录中可以看到的唯一可能导致错误的显而易见的事情就是'应该是'。有人可以帮助我吗?
答案 0 :(得分:0)
您的xml文档可能无效。无论如何,您可以设置将在第76次迭代时发生的断点并检查错误。
pS:在while语句中的某处设置断点。右键单击断点并选择HitCount。
答案 1 :(得分:0)
我在C#中编写了一个XML with XSD validator,可以很容易地适应跳过XSD,只检查XML是否至少是格式良好的。如果您有XSD架构,还可以检查XML是否有效。
Well formed XML遵守一些XML规则 Valid XML是格式良好的XML,也满足模式(XSD可用于制定这样的模式)
该工具将向您显示XML文档的位置不正确或无效。
最简单的方法是使用此工具download the whole bo-library source code tree,然后将bo-Library.sln
解决方案加载到Visual Studio中,选择ValidateXmlWithXsd.csproj
并从那里开始。