Hello all and thanks for the help, I need to write every row from one of my t-sql table columns into an xml file as a child. my current code OVER writes the last entry as it loops. The result should look like
(desired result)
<array>
<element>data1</element>
<element>data2</element>
<element>data3</element>
<element>data4</element>
<element>data5</element>
</array>
Not (currently)
<array>
<element>data5</element> (the last entry in table column)
</array>
What am I missing ? Thanks Again your time is much appreciated.
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);
}
}
You code is creating the file at the start of each iteration of the while loop (as well as closing an flushing it at the end of the while loop). Move that code outside the while loop and you'll have better results.