• 技术文章 >后端开发 >XML/RSS教程

    使用XmlTextWriter生成XML文件

    黄舟黄舟2017-02-13 15:47:36原创1320
    项目兼容需要生成一系列的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文件的内容,更多相关内容请关注PHP中文网(m.sbmmt.com)!

    声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。
    上一篇:XML数据读取方式性能比较(二) 下一篇:XML数据查询技术已经成为现今的研究热点
    php培训_php实战培训【立即报名】-php中文网第20期

    相关文章推荐

    • 【活动】充值PHP中文网VIP即送云服务器• xml约束技术之dtd的详解• XML数据查询技术已经成为现今的研究热点• Java中构造、生成XML简明教程• XML教程-XML的用途介绍• XML和Tomcat的入门知识的详细介绍
    1/1

    PHP中文网