private void btnmap_Click(object sender, EventArgs e)
{
XmlDocument xmldoc = new XmlDocument();
XmlNode xmlnode, xmlroot, docNode, Doc;
XmlAttribute xmlatt;
if (rchtextfile.Text == "")
{
MessageBox.Show("Please Select a Text file", "File Name Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else
{
con = new System.Data.SqlClient.SqlConnection();
DataSet ds = new DataSet();
con.ConnectionString = @"Server=sql1; User ID=blah; Pwd=fubar; Initial Catalog=xml;";
con.Open();
MessageBox.Show("Database Connected");
String sql = "select Styles from Xml_Tags,pdfelement where Xml_Tags.Mapping_Id=pdfelement.Mapping_Id AND Xml_Tags.Pdf_Tag=pdfelement.Element_Name AND pdfelement.Style=Xml_Tags.Styles";
com = new SqlCommand(sql);
da = new System.Data.SqlClient.SqlDataAdapter(sql, con);
da.Fill(ds, "xml");
maxrows = ds.Tables["xml"].Rows.Count;
StreamReader objReader = new StreamReader(file, Encoding.Default, true);
do
{
for (int i = 0; i < maxrows; i++)
{
docNode = xmldoc.CreateXmlDeclaration("1.0", "UTF-8", null);
//xmldoc.AppendChild(docNode);
dRow = ds.Tables["xml"].Rows[i];
line = objReader.ReadLine();
string st1 = ">";
string st2 = "</";
int end = line.IndexOf(st2);
if (end != -1 && end > 1)
{
st = line.IndexOf(st1);
en = line.IndexOf(st2);
int v = en - st;
sub = line.Substring(st + 1, v - 1);
rchtext.Text = rchtext.Text + sub + "\r\n";
}
String sqll = "select Dtd_Tag from Xml_Tags,pdfelement where Xml_Tags.Mapping_Id=pdfelement.Mapping_Id AND Xml_Tags.Pdf_Tag=pdfelement.Element_Name AND pdfelement.Style=Xml_Tags.Styles";
SqlCommand comm = new SqlCommand(sqll);
SqlDataAdapter daa = new System.Data.SqlClient.SqlDataAdapter(sqll, con);
DataSet ds1 = new DataSet();
daa.Fill(ds1, "xml");
dRow1 = ds1.Tables["xml"].Rows[i];
//String sqlll = "select Dtd_Attribute_Name from Mapped_Tags_Attributes,Xml_Tags where Mapped_Tags_Attributes.Pdf_Tag=Xml_Tags.Pdf_Tag";
String sqlll = "select Dtd_Attribute_Name from Mapped_Tags_Attributes,Xml_Tags where Mapped_Tags_Attributes.Pdf_Tag=Xml_Tags.Pdf_Tag AND Mapped_Tags_Attributes.Mapping_Id=Xml_Tags.Mapping_Id";
SqlCommand cmd = new SqlCommand(sqlll);
SqlDataAdapter dt = new System.Data.SqlClient.SqlDataAdapter(sqlll, con);
DataSet ds2 = new DataSet();
dt.Fill(ds2, "xml");
dRow2 = ds2.Tables["xml"].Rows[i];
name = XmlConvert.EncodeName(dRow1.ItemArray.GetValue(0).ToString());
xmlnode = xmldoc.CreateElement(name);
Doc = xmldoc.CreateDocumentType(name, null, "E:\\Rachana_mds\\proj\\pdfextraction\\docbook.dtd", null);
//xmldoc.AppendChild(Doc);
xmlroot = xmldoc.CreateElement(name);
//xmldoc.AppendChild(xmlroot);
xmlatt = xmldoc.CreateAttribute(dRow2.ItemArray.GetValue(0).ToString());
xmlroot.AppendChild(xmlnode);
xmlnode.InnerText = sub;
}
}
while (dRow[0].ToString() != line && objReader.Peek() != -1);
MessageBox.Show("Done XML")
saveFileDialog1.Filter = "XML Files (*.xml)|*.xml";
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
StreamWriter ya=new StreamWriter (saveFileDialog1.FileName.ToString());
xmldoc.Save(ya);
}
else
{
return;
}
}
MessageBox.Show("Successfully saved");
con.Close();
}
}
我在int end = line.IndexOf(st2);
行
答案 0 :(得分:6)
如果你正在NullReferenceException
int end = line.IndexOf(st2);
然后line
必须是null
。您应该找出原因 - 我的猜测是文件中的行数不如您预期的那么多。 (TextReader.ReadLine
在数据耗尽时返回null。)
此外:
using
和StreamReader
SqlConnection
语句
objReader.Peek
,而是检查行为空以检测输入结束答案 1 :(得分:0)
可能objReader.ReadLine();
返回null,所以
line = objReader.ReadLine();
不能按预期工作
答案 2 :(得分:0)
检查line
是否为null
:
int end;
if(line != null)
end= line.IndexOf(st2);
答案 3 :(得分:-1)
首先,尝试更好地格式化代码是不可读的。
问题是任何对象都没有初始化。