2
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();               
    }       
}

I am getting error at the int end = line.IndexOf(st2); line

1
  • does this really need a sql-server tag since the question is about c# code and not related to sql other than there's a sql statement in the code? Commented May 13, 2011 at 16:43

4 Answers 4

6

If you're getting a NullReferenceException on

int end = line.IndexOf(st2);

then line must be null. You should find out why - my guess is that there aren't as many lines in the file as you expect. (TextReader.ReadLine returns null when it's run out of data.)

Additionally:

  • This method is large, and hard to read. Try to refactor it into smaller chunks.
  • Use using statements for resources such as StreamReader and SqlConnection
  • Using LINQ to XML would probably make the XML part simpler
  • Rather than using objReader.Peek, check for the line being null to detect the end of input
  • (Relatively advanced) If this is in a client app, doing all of this work in the UI thread is going to lock up the UI. You should perform long-running tasks in a different thread.
Sign up to request clarification or add additional context in comments.

2 Comments

not to mention hard-coded connection strings, hard-coded file paths and inline sql..
@Richard: Yes, I thought it worth stopping after a while :)
0

probably objReader.ReadLine(); returns null, so

line = objReader.ReadLine();

doesn't work as supposed

1 Comment

I'd say maybe it doesn't work as "expected" by the OP, but certainly it does work as it is supposed to - it is perfectly OK for it to return null, when there are no more lines to read.
0

check that line is null or not:

int end;
if(line != null)
   end= line.IndexOf(st2);

Comments

-1

Firstly, Try to format a bit better the code is unreadeable.

The problem is that any of the object is not initialized.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.