大家好,感谢您的帮助,我小时候需要将我的一个 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);
}
}