我有一个sqlite数据库,有7个表。我需要将它们导出到c#.net中的XML文件。 这样做时,我使用嵌套的7个循环来访问7个数据表的数据,以维护列的依赖性。但是当我运行它时,我的机器变得很慢。它会生成XML文件,但生成的XML会在一段时间后发生变化。看起来,在每次迭代中,它都会生成一个XML并用新的XML替换它。我的代码如下:
public XMLSerializer(String filepath,ROOT root)
{
XmlDocument doc = new XmlDocument();
XmlNode rootNode, aNode, pNode, eNode, mNode, plNode;
// Create a procesing instruction.
XmlProcessingInstruction newPI;
String PItext = "type='text/xsl' href='root.xsl'";
newPI = doc.CreateProcessingInstruction("xml-stylesheet", PItext);
// Add the processing instruction node to the document.
doc.AppendChild(newPI);
rootNode = doc.CreateElement("root");
doc.AppendChild(rootNode);
plNode = doc.CreateElement("pl");
rootNode.AppendChild(plNode);
eNode = doc.CreateElement("e");
rootNode.AppendChild(eNode);
mNode = doc.CreateElement("m");
rootNode.AppendChild(mNode);
aNode = doc.CreateElement("a");
rootNode.AppendChild(aNode);
DataTable plDT = root.Getpl();
foreach (DataRow pRow in plDT.Rows)
{
XmlNode plNode = doc.CreateElement("p");
XmlNode pIDNode = doc.CreateElement("id");
XmlNode pteNode = doc.CreateElement("te");
XmlNode fNode = doc.CreateElement("fs");
plNode.AppendChild(pIDNode);
plNode.AppendChild(pteNode);
plNode.AppendChild(fNode);
plNode.AppendChild(plNode);
pIDNode.InnerText = pRow["id"].ToString();
pteNode.InnerText = root.Getpte(pRow["id"].ToString());
DataTable fmdt = root.GetFml(pRow["id"].ToString());
foreach (DataRow fmRow in fmdt.Rows)
{
XmlNode fsNode = doc.CreateElement("fm");
XmlNode fmIDNode = doc.CreateElement("id");
XmlNode fmpNode = doc.CreateElement("p");
XmlNode eNode = doc.CreateElement("e");
fsNode.AppendChild(fmIDNode);
fsNode.AppendChild(fmpNode);
fsNode.AppendChild(eNode);
plNode.AppendChild(fsNode);
fmIDNode.InnerText = fmRow["id"].ToString();
fmpNode.InnerText = pRow["id"].ToString();
DataTable eDT = root.Gete();
foreach (DataRow eRow in eDT.Rows)
{
XmlNode eNode = doc.CreateElement("e");
XmlNode eIDNode = doc.CreateElement("id");
XmlNode eDanNode = doc.CreateElement("dan");
XmlNode mNode = doc.CreateElement("m");
eNode.AppendChild(eIDNode);
eNode.AppendChild(edanNode);
//eNode.AppendChild(eNode);
fsNode.AppendChild(eNode);
eIDNode.InnerText = eRow["id"].ToString();
edanNode.InnerText = root.Getedan(eRow["id"].ToString());
DataTable mDT = root.Getm();
foreach (DataRow mRow in mDT.Rows)
{
XmlNode mNode = doc.CreateElement("m");
XmlNode mIDNode = doc.CreateElement("id");
XmlNode meNode = doc.CreateElement("e");
mNode.AppendChild(mIDNode);
mNode.AppendChild(meNode);
eNode.AppendChild(mNode);
mIDNode.InnerText = mRow["id"].ToString();
meNode.InnerText = root.Getme(mRow["id"].ToString());
DataTable aDT = root.Geta();
foreach (DataRow aRow in aDT.Rows)
{
XmlNode aNode = doc.CreateElement("a");
XmlNode AssIDNode = doc.CreateElement("id");
XmlNode AssNaNode = doc.CreateElement("na");
XmlNode pinNode = doc.CreateElement("pin");
aNode.AppendChild(AssIDNode);
aNode.AppendChild(AssNaNode);
aNode.AppendChild(pinNode);
AssIDNode.InnerText = aRow["id"].ToString();
AssNaNode.InnerText = aRow["na"].ToString();
mNode.AppendChild(aNode);
DataTable piDT = root.Getp(aRow["id"].ToString());
foreach (DataRow piRow in piDT.Rows)
{
XmlNode pInNode = doc.CreateElement("pIn");
XmlNode pInIDNode = doc.CreateElement("id");
XmlNode pInaNode = doc.CreateElement("a");
XmlNode pInpNode = doc.CreateElement("p");
XmlNode pInFminNode = doc.CreateElement("fmin");
pInNode.AppendChild(pInIDNode);
pInNode.AppendChild(pInaNode);
pInNode.AppendChild(pInpNode);
pInNode.AppendChild(pInFminNode);
aNode.AppendChild(pInNode);
pInIDNode.InnerText = piRow["id"].ToString();
pInaNode.InnerText = aRow["id"].ToString();
pInpNode.InnerText = pRow["id"].ToString();
DataTable fmiDT = root.GetFmin(piRow["id"].ToString());
foreach (DataRow fmiRow in fmiDT.Rows)
{
XmlNode fmInNode = doc.CreateElement("fmIn");
XmlNode fmInIDNode = doc.CreateElement("id");
XmlNode fmInpInNode = doc.CreateElement("pIn");
XmlNode fmInFmNode = doc.CreateElement("fm");
XmlNode fmIneNode = doc.CreateElement("e");
XmlNode fmInmNode = doc.CreateElement("m");
fmInNode.AppendChild(fmInIDNode);
fmInNode.AppendChild(fmInpInNode);
fmInNode.AppendChild(fmInFmNode);
fmInNode.AppendChild(fmIneNode);
fmInNode.AppendChild(fmInmNode);
pInNode.AppendChild(fmInNode);
fmInIDNode.InnerText = fmiRow["id"].ToString();
fmInpInNode.InnerText = piRow["id"].ToString();
fmInFmNode.InnerText = fmRow["id"].ToString();
fmIneNode.InnerText = eRow["id"].ToString();
fmInmNode.InnerText = mRow["id"].ToString();
}
}
}
}
}
}
}
doc.Save(filepath);
}
}
}
我认为我的编码方式效率不高。有谁能告诉我任何访问数据表的更好方法?
答案 0 :(得分:0)
您只需使用DataTable.WriteXml():
即可YourDataTable.WriteXml(strfilepath);
或
YourDataSet.WrteXml(strfilepath);