微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用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文件内容,更多相关内容请关注编程之家(www.PHP.cn)!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。