大家好,感谢您的帮助, 我需要将我的一个t-sql表列中的每一行写成一个xml文件作为子项。我当前的代码OVER在循环时写入最后一个条目。结果应该看起来像
(desired result)
<array>
<element>data1</element>
<element>data2</element>
<element>data3</element>
<element>data4</element>
<element>data5</element>
</array>
不(目前)
<array>
<element>data5</element> (the last entry in table column)
</array>
我错过了什么? 再次感谢您的时间,非常感谢。
public void Download_Click(object sender, EventArgs e)
{
string AddressVal;
string invoiceVal;
SqlConnection conn = null;
SqlConnection connTwo = null;
try
{
//**************************************
string connStr2 = ConfigurationManager.ConnectionStrings["MYConString"].ConnectionString;
string cmdStr2 = "select * from t_BannerUser";
connTwo = new SqlConnection(connStr2);
SqlCommand oleComm2 = new SqlCommand(cmdStr2, connTwo);
connTwo.Open();
SqlDataReader oleReader2 = oleComm2.ExecuteReader();
//************************************** DATATABLE TEST *************
//SqlDataAdapter da = new SqlDataAdapter(cmdStr2, connStr2);
//DataTable dt = new DataTable();
//da.Fill(dt);
//for (int i = 0; i < dt.Rows.Count; i++)
//{
//****************************
while (oleReader2.Read())
{
// AddressVal = oleReader["c_user"].ToString();
invoiceVal = oleReader2["c_user_id"].ToString();
//create XMLTextWriter object and set its save location
//…the second value is for an Encoding declaration I found to be unnecessary, hence, null
XmlTextWriter plist = new XmlTextWriter("C:/Temp/plistName.plist", null);
//assign basic formatting to the XMLTextWriter so that the XML is easily legible
plist.Formatting = Formatting.Indented;
//default indentation is 2 spaces; 4 spaces is essentially one “tab”
plist.Indentation = 4;
//create the initial xml element to start the document
plist.WriteProcessingInstruction("xml", "version=\"1.0\" encoding=\"UTF-8\"");
//create a custom DTD
plist.WriteDocType("plist", "-//Apple//DTD PLIST 1.0//EN", "http://www.apple.com/DTDs/PropertyList-1.0.dtd", null);
//create root element
plist.WriteStartElement("plist");
//assign version attribute
plist.WriteAttributeString("version", "1.0");
//creating a dict element before anything else is required
plist.WriteStartElement("dict");
//create the array key (shows up as the array name when viewing the .plist)
plist.WriteElementString("key", "ArrayName");
//you have to then create the array!
plist.WriteStartElement("array");
//create a dict element to arrange succeeding object attributes
plist.WriteStartElement("dict");
//create key element (shows as item name when viewing the plist)
plist.WriteElementString("key", "KeyName");
//create string element (shows as item value when viewing the plist)
//plist.WriteElementString("string", dt.Rows[i]["c_user_id"].ToString());
plist.WriteElementString("string", invoiceVal.ToString());
//create the array key (shows up as the array name when viewing the plist)
plist.WriteElementString("key", "ArrayName");
//you have to then create the array!
plist.WriteStartElement("array");
//create a dict element to arrange succeeding object attributes
plist.WriteStartElement("dict");
//create key element (shows as item name when viewing the plist)
plist.WriteElementString("key", "KeyName");
//create string element (shows as item value when viewing the plist)
//plist.WriteElementString("string", DataTable.Rows[int]["ColumnName"].ToString());
plist.WriteElementString("string", invoiceVal.ToString());
plist.Flush();
plist.Close();
// }
}
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
}
}
答案 0 :(得分:3)
您的代码是在while循环的每次迭代开始时创建文件(以及在while循环结束时关闭它)。将该代码移到while循环之外,您将获得更好的结果。