0

我很想导入一个包含大量特殊字符的大型 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 ' 应该在哪里。有人可以帮帮我吗?

4

2 回答 2

0

您的 xml 文档可能无效。无论如何,您可以设置将在第 76 次迭代时发生的断点并检查错误。

pS:在while语句的某处设置断点。右键单击断点并选择 HitCount。

于 2012-10-23T03:58:56.623 回答
0

不久前,我用 C# 编写了一个带有 XSD 验证器的 XML,它可以很容易地适应跳过 XSD,并且只检查 XML 是否至少格式正确。如果您有 XSD 架构,您还可以检查 XML 是否有效。

格式良好的 XML遵循一些 XML 规则
有效的 XML是格式良好的 XML,也满足模式(XSD 可用于制定这样的模式)

该工具将向您显示它在 XML 文档的哪个位置格式不正确或无效。

让这个工具工作的最简单方法就是下载整个 bo-library 源代码树,然后将bo-Library.sln解决方案加载到 Visual Studio 中,选择ValidateXmlWithXsd.csproj并从那里开始。

于 2012-10-23T15:27:46.520 回答