向XML文件写入属性

本文关键字:属性 文件 XML | 更新日期: 2023-09-27 18:04:04

我想做一个横幅旋转与ASP。使用Microsoft Visual Studio 2010和MSSQL Server开发。Net c#。我已经写了一个XML这些项目是记录在数据库中。但是,我不能给WriteStartElement("properties")

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>

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>

向XML文件写入属性

我将使用XmlDocumentXmlNode类。这样就可以向XmlNodes添加属性,然后向文档添加XmlNodes:

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

如果我理解正确的话,你是想给banner标签添加属性。

XmlWriter.WriteAttributeString()

方法可以做到这一点。就在你呼叫

之后
xmlyazici.WriteStartElement("banner");

使用如下方法:

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