2022年使用C#生成XML的两种方式知识 .pdf
一、使用 XmlWriter创建 XML 文件:1.usingSystem;2.3.usingSystem.Collections.Generic;4.5.usingSystem.Text;6.7.usingSystem.IO;8.9.usingSystem.Xml;10.11.12.13.namespaceUseXmlWriter14.15.16.17.classProgram18.19.20.21.staticvoidMain(stringargs)22.23.24.25.using(MemoryStreamms = new MemoryStream()26.27.28.29.XmlWriterSettingssettings= new XmlWriterSettings();30.31./ 要求缩进32.33.settings.Indent=true;34.35./ 注意如果不设置encoding默认将输出utf-1636.37./ 注意这儿不能直接用Encoding.UTF8如果用 Encoding.UTF8将在输出文本的最前面添加4 个字节的非xml 内容38.39.settings.Encoding=new UTF8Encoding(false);名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 1 页,共 8 页 - - - - - - - - - 40.41.42.43./ 设置换行符44.45.settings.NewLineChars= Environment.NewLine;46.47.48.49.using(XmlWriterxmlWriter= XmlWriter.Create(ms,settings)50.51.52.53.54.55./ 写 xml文件开始56.57.xmlWriter.WriteStartDocument(false);58.59./ 写根节点60.61.xmlWriter.WriteStartElement(root);62.63./ 写字节点64.65.xmlWriter.WriteStartElement(cat);66.67./ 给节点添加属性68.69.xmlWriter.WriteAttributeString(color,white);70.71./ 给节点内部添加文本72.73.xmlWriter.WriteString(Ima cat);74.75.xmlWriter.WriteEndElement();76.77.78.79.80.名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 2 页,共 8 页 - - - - - - - - - 81./ 通过 WriteElementString可以添加一个节点同时添加节点内容82.83.xmlWriter.WriteElementString(pig,pigisgreat );84.85.86.87.88.89.xmlWriter.WriteStartElement(dog);90.91./ 写 CData92.93.xmlWriter.WriteCData(dogisdog );94.95.xmlWriter.WriteEndElement();96.97.98./ 添加注释99.xmlWriter.WriteComment(thisisanexamplewrited );100.101.102.103.xmlWriter.WriteEndElement();104.105.xmlWriter.WriteEndDocument();106.107.108.109.110.111.112.113./ 将 xml 内容输出到控制台中114.115.stringxml= Encoding.UTF8.GetString(ms.ToArray();116.117.Console.WriteLine(xml);118.119.名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 3 页,共 8 页 - - - - - - - - - 120.121.Console.Read();122.123.124.125.126.127.128.129.二、使用 LinqTo XML 创建 RSS 源文件:1.usingSystem;2.3.4.usingSystem.Web;5.6.7.usingSystem.Xml;8.9.10.usingSystem.Xml.Linq;11.12.13.14.15.namespaceWebApplication116.17.18.19.20.21.publicclassRssHttpHandler:IHttpHandler22.23.24.25.26.27.28.29.publicvoidProcessRequest(HttpContextcontext)30.名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 4 页,共 8 页 - - - - - - - - - 31.32.33.34.35.context.Response.ContentType=text/xml;36.37.38.39.40.context.Response.Write( );41.42.43.44.45.XElementrssFeed=newXElement(rss,newXAttribute(version,2.0);46.47.48.49.50.stringfixedUrl= http:/ string.Empty;54.55.56.57.58.XElementchannel=newXElement(channel,59.60.61.new XElement(title,freeflying),62.63.64.new XElement(link,fixedUrl),65.66.67.new XElement(description,thewebsitefordreamflyingfreely),68.69.70.new XElement(pubDate,DateTime.Now.ToString()71.名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 5 页,共 8 页 - - - - - - - - - 72.73.);74.75.76.77.78.79.80.foreach(vararticleinArticles.GetArticles()81.82.83.84.85.86.XElementitem= newXElement(item);87.88.89.90.91.XElementtitle=new XElement(title,article.Title);92.93.94.95.96.wholeUrl= string.Format(0?id=1&catelog=2,fixedUrl,article.ID,article.Catelog);97.98.99.XElementlink= newXElement(link,wholeUrl);100.101.102.103.104.XElementdescription= newXElement(description,article.Description);105.106.107.108.109.XElementpubDate= new XElement(pubDate,article.LastMod.ToString();110.111.名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 6 页,共 8 页 - - - - - - - - - 112.113.114.item.Add(title,link,description,pubDate);115.116.117.118.119.channel.Add(item);120.121.122.123.124.125.126.127.rssFeed.Add(channel);128.129.130.131.132.context.Response.Write(rssFeed);133.134.135.136.137.138.139.140.141.142.publicboolIsReusable143.144.145.146.147.148.getreturnfalse;149.150.151.152.153.154.155.名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 7 页,共 8 页 - - - - - - - - - 156.157.名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 8 页,共 8 页 - - - - - - - - -