我正在嘗試使用Microsoft Visual Studio 2010和MSSQL Server來做一個帶有ASP.Net C#的橫幅輪播。我已經寫入了XML中那些記錄在數據庫中的項目。但是,我不能給WriteStartElement(「屬性」)。將屬性寫入XML文件
XML代碼:上部代碼
XmlTextWriter xmlyazici = new XmlTextWriter(Server.MapPath("banner.xml"), Encoding.UTF8);
xmlyazici.WriteStartDocument();
SqlConnection baglanti = new SqlConnection(ConfigurationManager.ConnectionStrings["baglan"].ConnectionString);
baglanti.Open();
string sql = "SELECT TOP 6 ID,RESIM,URL,DURATION FROM REKLAMLAR ORDER BY REKLAMLAR.ID DESC";
SqlCommand komut = new SqlCommand(sql, baglanti);
SqlDataReader dr = komut.ExecuteReader();
xmlyazici.WriteStartElement("banner"); // aşağıdaki örnekteki gibi özellikler atamak istiyorum(Yapmaya Çalıştığım xml Çıktı Kısmı gibi).
while (dr.Read())
{
xmlyazici.WriteStartElement("item");
xmlyazici.WriteElementString("path", "images/" + dr.GetString(1) + "");
xmlyazici.WriteElementString("link", "" + dr.GetString(2) + "");
xmlyazici.WriteElementString("bar_color", "0xffffff");
xmlyazici.WriteElementString("bar_transparency", "40");
xmlyazici.WriteElementString("caption_color", "0xffffff");
xmlyazici.WriteElementString("caption_transparency", "60");
xmlyazici.WriteElementString("stroke_color", "0xffffff");
xmlyazici.WriteElementString("stroke_transparency", "60");
xmlyazici.WriteElementString("slideshowTime", "" + dr.GetString(3) + "");
xmlyazici.WriteEndElement();
}
dr.Close();
baglanti.Close();
xmlyazici.WriteEndElement();
xmlyazici.WriteEndDocument();
xmlyazici.Flush();
xmlyazici.Close();
XML結果:
<banner>
<item>
<path>images/72815305878.jpg</path>
<link>http://www.xxxxxxx.com/default.aspx</link>
<bar_color>0xffffff</bar_color>
<bar_transparency>40</bar_transparency>
<caption_color>0xffffff</caption_color>
<caption_transparency>60</caption_transparency>
<stroke_color>0xffffff</stroke_color>
<stroke_transparency>60</stroke_transparency>
<slideshowTime>20</slideshowTime>
</item>
</banner>
我想要做的事情:
***<banner width = "" height = ""
startWith = "1"
random = "false">***
<item>
<path>images/72815305878.jpg</path>
<link>http://www.xxxxx.com/default.aspx</link>
<bar_color>0xffffff</bar_color>
<bar_transparency>40</bar_transparency>
<caption_color>0xffffff</caption_color>
<caption_transparency>60</caption_transparency>
<stroke_color>0xffffff</stroke_color>
<stroke_transparency>60</stroke_transparency>
<slideshowTime>20</slideshowTime>
</item>
</banner>
你不應該使用'new XmlTextWriter()'。從.NET 2.0開始已經被棄用了。改爲使用'XmlWriter.Create()'。 –