我只是試圖加載數據集並將其作爲XML輸出到網頁中,同時還要寫入模式。我一直在研究找到一種方法來實現這一點,沒有任何運氣。ASP 4.0,C#將數據集寫入XML數據
我使用的代碼是:
string str =
"SELECT Name,Members,MaxLvl,Faction,Government,Score FROM dim5orgs where faction =
'Omni' order by Score DESC";
// Connection string for a typical local MySQL installation
string cnnString = "Server=xxxxxxxnet;Port=3306;Database=xxx;Uid=xxxxx;Pwd=xxxx";
// Create a connection object and data adapter
MySqlConnection cnx = new MySqlConnection(cnnString);
MySqlDataAdapter adapter = new MySqlDataAdapter();
// Create a SQL command object
string cmdText = str;
MySqlCommand cmd = new MySqlCommand(cmdText, cnx);
// Create a fill a Dataset
DataSet ds = new DataSet();
adapter.SelectCommand = cmd;
adapter.Fill(ds);
StringWriter sw = new StringWriter();
ds.WriteXml(sw,XmlWriteMode.WriteSchema);
string result = sw.ToString();
Response.Write(result);
現在我得到的輸出,如:
Punk732220OmniRepublic1644786805740
Paradise754220OmniDepartment1633903815782
我想輸出是正確的XML形式以某種方式使用的列名數據集。
像:
<data>
<name>Punk</name>
<members>732</members>
<Maxlvl>220</MaxLvl>...etc
</data>
我想在適當的XML形式,書面正確的XML頭。
謝謝。
邁克爾,下面的答案中的任何一個回答你的問題 - 如果其中之一,你能把它標記爲接受嗎? – 2014-02-12 16:07:16