该文档已经有一个'XmlDeclaration'节点

时间:2011-05-06 09:05:35

标签: c# .net sql-server

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

2 个答案:

答案 0 :(得分:3)

您只需要文档中的1个xmldeclaration节点。你试图为循环的每次迭代添加一个..

答案 1 :(得分:2)

使用for循环i=0此代码运行

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

并向XMLDeclaration添加xmldoc,所有内容都以i=0的for循环结束,然后i变为1 i++之后,相同的for循环适用于i=1,此代码再次起作用

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

尝试将另一个XMLDeclaration添加到xmldoc并提供您提到的错误。如何摆脱这个错误就是编写这段代码

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

在您的循环外fordo-while根据您的需要,您可以使用此代码

XmlDocument xmldoc = new XmlDocument(); 

就在这之前

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

但是如果你没有将它们保存在xmldoc中,那么你将拥有这么多List并且你会失去它们。

除了上述所有内容之外,我建议您编写更多面向对象的代码。例如,您可以为for循环创建另一种方法。尝试分离您的方法,分而治之。