开发者

Invalid XML document, The document does not have a root element [closed]

开发者 https://www.devze.com 2023-03-05 21:04 出处:网络
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current form. For help clari
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.
 private void btnmap_Click(object sender, EventArgs e)
 { 
 XmlDocument xmldoc = new XmlDocument();
                 XmlNode xmlnode, xmlroot, docNode, Doc;
                 XmlAttribute xmlatt;
                 docNode = xmldoc.CreateXmlDeclaration("1.0", "UTF-8", null);
                 xmldoc.AppendChild(docNode);                
                 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=MDS-SW02; User ID=sa; Pwd=Admin2011; 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++)
                         {                           
                             dRow = ds.Tables["xml"].Rows[i];                             
                             line = objReader.ReadLine();
                             if (line == null)
                             {
                                 //xmldoc.Save(ya);
                             }
                             else
                             {
                                 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 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);                                
                                 xmlroot = xmldoc.CreateElement(name);                                 
                                 xmlatt = xmldoc.CreateAttribute(dRow2.ItemArray.GetValue(0).ToString());
                                 xmlroot.AppendChild(xmlnode);
                                 xmlnode.InnerText = sub;                                                                
                             }
                         }
                     }                    
                     while (dRow[0].ToString()!=line && !objReader.EndOfStream);
                         MessageBox.Show("Done");
                         string filename = @"E:" + DateTime.Now.Day + DateTime.Now.Month + DateTime.Now.Minute + ".xml";                         
                         xmldoc.Save(filename);                                                  
                         MessageBox.Show("Successfully saved");                                              
                 }                
                 con.Close();              
}

I am getting error for this line. ...xmldoc.Save(filename);


You're never calling doc.AppendChild(xmlroot); so although you've created the elements, you're never really putting them in the document. Hence it doesn't have a root element.

Now if you did create the root element and add it, you'd still have a problem if the for loop executed more than once, as you'd be trying to add multiple roots. You probably want to create xmlroot outside the loop, and then create the nested elements inside the loop.

If you can use .NET 3.5 or higher and LINQ to XML, a lot of this code would be a lot simpler, by the way...


The below line is also incorrect:

string filename = @"E:" + DateTime.Now.Day + DateTime.Now.Month + DateTime.Now.Minute + ".xml";                    

replace the line with:

string filename = @"E:\\" + DateTime.Now.Day + DateTime.Now.Month + DateTime.Now.Minute + ".xml";                              
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号