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文件的内容。
