开发者

Writing Attributes to an XML File

开发者 https://www.devze.com 2023-03-23 19:25 出处:网络
I\'m trying to doa Banner Rotator with ASP.Net C# by using Microsoft Visual Studio 2010 and MSSQL Server.I have written to a XML those items which are record within database for that.But,I can\'t give

I'm trying to do a Banner Rotator with ASP.Net C# by using Microsoft Visual Studio 2010 and MSSQL Server.I have written to a XML those items which are record within database for that.But,I can't give WriteStartElement("properties").

XML Code:

        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 result of upper code:

   <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>

I wanna do thing:

    ***<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>


I would use XmlDocument and XmlNode classes. That way you can add attributes to the XmlNodes, then add XmlNodes to the document:

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

Hope this helps.


If I understand you correctly, you want to add attributes to the banner tag.

XmlWriter.WriteAttributeString()

method does that. Right after you call

xmlyazici.WriteStartElement("banner");

use this method like:

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

精彩评论

暂无评论...
验证码 换一张
取 消