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);
線對象引用未設置到對象
這是否真的需要一個SQL服務器標籤因爲問題是關於C#代碼,而不是與sql有關,除了在代碼中有一個SQL語句? – DForck42 2011-05-13 16:43:58