使用XmlTextWriter生成XML文件

項目兼容需要生成一系列的xml文件,總結了下XML文件的生成基本方式
XmlTextWriter w = new XmlTextWriter("C:\XML文件名.xml", Encoding.Unicode); //Encoding.Unicode爲生成XML文件的編碼格式,到時候合輸出:<?xml version="1.0" encoding="utf-16"?>
w.Formatting = Formatting.Indented; // 這個比較重要,這個屬性說明xml文件裏面的內容是按級別縮進的。
//下面開始生成文件的內容
w.WriteStartDocument(); //開始寫xml,在最後有一個與之匹配的w.WriteEndDocument();
w.WriteStartElement("SpotList");
 w.WriteAttributeString("xmlns:xsi", "http:www.w3.org/2001/XMLSchema-instance"); //SpotList節點的屬性
 w.WriteAttributeString("xmlns:xsd", "http:www.w3.org/2001/XMLSchema"); //SpotList節點屬性,最後效果:<SpotList xmlns:xsi="http:www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http:www.w3.org/2001/XMLSchema">
 w.WriteStartElement("Items");
  w.WriteElementString("Name", myPoints[j].Name);
  w.WriteElementString("Caption", myPoints[j].Caption);
  w.WriteElementString("Addr", myPoints[j].Addr);
  w.WriteElementString("Phone", myPoints[j].Phone);
  w.WriteStartElement("Intro"); //最後效果:<Intro><![CDATA[相關內容]]></Intro>
   w.WriteCData(myPoints[j].Intro);
  w.WriteEndElement();
 w.WriteEndElement();
w.WriteEndElement();
w.WriteEndDocument();
w.Close(); //完成xml文件的輸出,關閉
-

資料引用:http://www.knowsky.com/442566.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章