此文档已有一个“XmlDeclaration”节点
本文关键字:XmlDeclaration 节点 有一个 文档 | 更新日期: 2023-09-27 17:55:19
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();
文档中只需要 1 个 xmldeclaration 节点。您正在尝试为循环的每次迭代添加一个。
for
循环与此代码运行i=0
XmlNode docNode = xmldoc.CreateXmlDeclaration("1.0", "UTF-8", null);
xmldoc.AppendChild(docNode);
并向xmldoc
添加一个XMLDeclaration
,所有内容都以 for i=0
循环结束,然后 i
变得1
i++
之后相同的for
循环适用于i=1
,这段代码再次工作
XmlNode docNode = xmldoc.CreateXmlDeclaration("1.0", "UTF-8", null);
xmldoc.AppendChild(docNode);
它试图向xmldoc
添加另一个XMLDeclaration
,并给出您提到的错误。要摆脱此错误,您可以做的是编写此代码
XmlNode docNode = xmldoc.CreateXmlDeclaration("1.0", "UTF-8", null);
xmldoc.AppendChild(docNode);
在循环之外for
和do-while
,根据你想要的,你可以采用这个代码
XmlDocument xmldoc = new XmlDocument();
就在这之前
XmlNode docNode = xmldoc.CreateXmlDeclaration("1.0", "UTF-8", null);
xmldoc.AppendChild(docNode);
但是你会有很多xmldoc
,如果你不把它们保存在List
中,你就会失去它们.
除了上述所有内容之外,我建议您编写更面向对象的代码。例如,您可以为该for
循环创建另一个方法。试着把你的方法分开,分而治之。