开发者

This document already has an 'XmlDeclaration' node

开发者 https://www.devze.com 2023-03-03 21:58 出处:网络
XmlDocument xmldoc = new XmlDocument(); XmlNode xmlnode, xmlroot; con = new System.Data.SqlClient.SqlConnection();
XmlDocument xmldoc = new XmlDocument();
XmlNode xmlnode, xmlroot;                  
con = new System.Data.SqlClient.SqlConnection();
DataSet ds = new DataSet();                
con.ConnectionString = @"...snip...";
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();
    开发者_如何学Python    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)
                {

                    XmlNode docNode = xmldoc.CreateXmlDeclaration("1.0", "UTF-8", null);
                    xmldoc.AppendChild(docNode);

                    String sqll = "select Dtd_Tag,Dtd_Attribute_Name from Xml_Tags,Mapped_Tags_Attributes where Mapped_Tags_Attributes.Pdf_Tag=Xml_Tags.Pdf_Tag";
                    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 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);

                    String sqlll = "select Dtd_Attribute_Name,Dtd_Tag from Mapped_Tags_Attributes,Xml_Tags where Mapped_Tags_Attributes.Pdf_Tag=Xml_Tags.Pdf_Tag";
                    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];

                    xmlroot = xmldoc.CreateElement(dRow1.ItemArray.GetValue(0).ToString());
                    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();


You only need 1 xmldeclaration node in the document. You're attempting to add one for each iteration of the loop..


with your for loop with i=0 this code runs

XmlNode docNode = xmldoc.CreateXmlDeclaration("1.0", "UTF-8", null);
xmldoc.AppendChild(docNode);

and adds an XMLDeclaration to xmldoc, all things end in the for loop with i=0, then i becomes 1 with i++ afterwards the same for loop works for i=1 and this code works again

XmlNode docNode = xmldoc.CreateXmlDeclaration("1.0", "UTF-8", null);
xmldoc.AppendChild(docNode); 

which tries to add another XMLDeclaration to xmldoc and gives you that error you mentioned. What you can do to get rid of this error is writing this code

XmlNode docNode = xmldoc.CreateXmlDeclaration("1.0", "UTF-8", null);
xmldoc.AppendChild(docNode); 

outside your loops both for and do-while and based on what you want you can take this code

XmlDocument xmldoc = new XmlDocument(); 

just before this one

XmlNode docNode = xmldoc.CreateXmlDeclaration("1.0", "UTF-8", null);
    xmldoc.AppendChild(docNode); 

but then you will have so many xmldoc and you will lose them if you don't save them in a List.

In addition to all those above, I suggest you to write your codes more object-oriented.You can create another method for that for loop for example.Try to seperate your methods, divide and conquer.

0

精彩评论

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

关注公众号