2011-07-28 80 views
0

我正在嘗試使用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> 
+1

你不應該使用'new XmlTextWriter()'。從.NET 2.0開始已經被棄用了。改爲使用'XmlWriter.Create()'。 –

回答

1

我會用XmlDocumentXmlNode類。您可以將屬性添加到將XMLNode這樣,再加入到的XMLNodes文件:

XmlDocument doc = new XmlDocument(); 
XmlNode root = doc.CreateElement("banner"); 
((XmlElement)root).SetAttribute("attribute-name", "attribute value"); 
doc.AppendChild(root); 

希望這有助於。

+0

首先,感謝您的有趣。我可以使用您的代碼爲橫幅廣告代碼添加屬性? – Selo

+0

我上面發佈的代碼示例顯示瞭如何將任何屬性添加到任何節點。在MSDN上查看這些示例: http://social.msdn.microsoft.com/Forums/en-US/xmlandnetfx/thread/4b8adf16-37fa-4715-bbe2-1f0756ff29cf http://msdn.microsoft.com/ en-us/library/system.xml.xmlnode.attributes.aspx – woohoo

+0

再次感謝您的所有評論。 – Selo

0

如果我理解正確的話,你要添加的屬性到橫幅標籤。

XmlWriter.WriteAttributeString() 

這樣做。 你叫

xmlyazici.WriteStartElement("banner"); 

使用這種方法等之後,右:

xmlyazici.WriteAttributeString("width", ""); 
xmlyazici.WriteAttributeString("height", ""); 
xmlyazici.WriteAttributeString("startWith", "1"); 
xmlyazici.WriteAttributeString("random", "false");