使用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文件的輸出,關閉
總結
以上是生活随笔為你收集整理的使用XmlTextWriter生成XML文件的方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 态度决定高度
- 下一篇: asp.net ajax控件工具集 Au