找不到第2列

时间:2011-05-05 11:03:04

标签: c# .net sql-server

private void btnmap_Click(object sender, EventArgs e)
{               
   XmlDocument xmldoc = new XmlDocument();                
   con = new System.Data.SqlClient.SqlConnection();
   ds = new DataSet();
   con.ConnectionString = @"Server=MDS-SW02; User ID=sa; Pwd=Admin2011;Initial Catalog=xml;";
   con.Open();
   MessageBox.Show("Database Connected");                
   String sql = "select Pdf_tag,Styles from Xml_Tags";              
   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
   {                                           
      string 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"; 
      }                     
      for (int i = 0; i < maxrows; i++)
      {
          dRow = ds.Tables["xml"].Rows[i];
          if(dRow[0].ToString ()==line)
          {
              //XmlDocument xmldoc = new XmlDocument();
              XmlNode docNode = xmldoc.CreateXmlDeclaration("1.0", "UTF-8", null);
              xmldoc.AppendChild(docNode);
              XmlNode xmlnode,xmlroot;

              String sqll = "select Dtd_Tag from Xml_Tags where Mapped_Tags_Attributes.Pdf_Tag=Xml_Tags.Pdf_Tag";
              SqlCommand comm = new SqlCommand(sqll);
              SqlDataAdapter daa = new System.Data.SqlClient.SqlDataAdapter(sqll, con);
              daa.Fill(ds1, "xml");
              dRow1 = ds1.Tables["xml"].Rows[i];

              string name = XmlConvert.EncodeName(dRow1.ItemArray.GetValue(0).ToString());
              xmlnode = xmldoc.CreateElement(name);

              XmlNode Doc = xmldoc.CreateDocumentType(name, null, "E:\\Rachana_mds\\proj\\pdfextraction\\docbook.dtd", null);
              xmldoc.AppendChild(Doc);

              xmlroot = xmldoc.CreateElement(dRow1.ItemArray.GetValue(0).ToString());
              xmldoc.AppendChild(xmlroot);             

              String sqlll = "select Dtd_Attribute_Name from Mapped_Tags_Attributes where Mapped_Tags_Attributes.Pdf_Tag=Xml_Tags.Pdf_Tag";
              SqlCommand cmd = new SqlCommand(sqlll);
              SqlDataAdapter dt = new System.Data.SqlClient.SqlDataAdapter(sqlll,con);
              dt.Fill(ds2, "xml");
              dRow2 = ds2.Tables["xml"].Rows[i];

              xmlroot = xmldoc.CreateElement(name);
              xmldoc.AppendChild(xmlroot);
              XmlAttribute xmlatt = xmldoc.CreateAttribute(dRow2.ItemArray.GetValue(0).ToString());                                              
              xmlroot.AppendChild(xmlnode);
              xmlnode.InnerText=sub;                       
            }                                                                
            while (objReader.Peek() != -1);
            //string filename = @"E:" + DateTime.Now.Day + DateTime.Now.Month + DateTime.Now.Minute + ".xml";
            string filename = @"E:\" + DateTime.Now.ToString("dd/mm/yyyy")+".xml";
            xmldoc.Save(filename); 
            MessageBox.Show("Done");
            con.Close();                                  
}

1 个答案:

答案 0 :(得分:3)

啊,既然你已告诉我们错误(评论)这一行,问题很明显:

dRow = ds.Tables["xml"].Rows[i];
if(dRow[i].ToString ()==line) {...}

dRow是一排;除非您的数据是纯方形,否则您可能打算访问每行的第一列:

dRow = ds.Tables["xml"].Rows[i];
if(dRow[0].ToString ()==line) {...}