栏目导航
热点推荐
- 用JDOM创建XML文件源代码
- 利用XML和XSL来生成动态页面
- WAP网站建设全攻略教程
- XML结构与语法入门详解
- XSLT Designer快速入门指南
- XML入门的常见问题(三)
- XML入门的常见问题(四)
- XMLHTTPRequest属性及方法详解教
- 用XML和XSL来生成动态页面
- XML教程:XML有哪些用途?
- 什么是 XML Web Service
- 如何建立一个 XML 的开发环境
阅览排行
使用XmlTextWriter生成XML文件
www.jz123.cn 2009-01-12 来源: 中国建站 编辑整理 我要投递新闻
项目兼容需要生成一系列的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 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文件的输出,关闭
0
上一篇:如何用ASP生成XML数据文档 下一篇:XMLHTTPRequest对象