站长资源网络编程

通过C#动态生成图书信息XML文件

整理:jimmy2026/11/2浏览2次
简介通过C#动态生成图书信息XML文件(Books.xml),文件如下: 复制代码 代码如下: Harry P</div> <div class="news_infos"><div id="MyContent">通过C#动态生成图书信息XML文件(Books.xml),文件如下: <BR><U>复制代码</U> 代码如下: <BR><?xml version="1.0" encoding="iso-8859-1"?> <BR><bookstore> <br><br><book id="1" category="CHILDREN"> <BR><title>Harry Potter</title> <BR><author>J K. Rowling</author> <BR><publishDate>2005-08-15</publishDate> <BR><price>29.99</price> <BR></book> <br><br><book id="2" category="WEB"> <BR><title>Learning XML</title> <BR><author>Erik T. Ray</author> <BR><publishDate>2003-10-18</publishDate> <BR><price>39.95</price> <BR></book> <br><br></bookstore> <BR> <BR><STRONG>方法1:使用StringBuilder拼接XML</STRONG> <BR><U>复制代码</U> 代码如下: <BR>/// <summary> <BR>/// 创建图书信息XML <BR>/// </summary> <BR>public void CreateBookXML(string fileName) <BR>{ <BR>StringBuilder xmlResult = new StringBuilder("<?xml version=\"1.0\" encoding=\"utf-8\"?>"); <BR>List<BookInfo> bookList = GetBookList(); //获取图书列表 <BR>if (bookList != null && bookList.Count > 0) <BR>{ <BR>xmlResult.Append("<bookstore>"); <BR>foreach (BookInfo book in bookList) <BR>{ <BR>xmlResult.AppendFormat("<book id=\"{0}\" category=\"{1}\">", book.BookId, book.Category); <BR>xmlResult.AppendFormat("<title>{0}</title>", book.Title); <BR>xmlResult.AppendFormat("<author>{0}</author>", book.Author); <BR>xmlResult.AppendFormat("<publishDate>{0}</publishDate>", book.PublishDate.ToString("yyyy-MM-dd")); <BR>xmlResult.AppendFormat("<price>{0}</price>", book.Price); <BR>xmlResult.Append("</book>"); <BR>} <BR>xmlResult.Append("</bookstore>"); <BR>} <br><br>//写入文件 <BR>try <BR>{ <BR>//1.创建文件流 <BR>FileStream fileStream = new FileStream(fileName, FileMode.Create); <BR>//2.创建写入器 <BR>StreamWriter streamWriter = new StreamWriter(fileStream); <BR>//3.将内容写入文件 <BR>streamWriter.WriteLine(xmlResult); <BR>//4.关闭写入器 <BR>streamWriter.Close(); <BR>//5.关闭文件流 <BR>fileStream.Close(); <BR>} <BR>catch (Exception e) <BR>{ } <BR>} <BR> <BR><STRONG>方法2:使用XmlTextWriter类创建XML</STRONG> <BR><U>复制代码</U> 代码如下: <BR>/// <summary> <BR>/// 创建图书信息XML <BR>/// </summary> <BR>/// <param name="fileName"></param> <BR>public void CreateBookXML(string fileName) <BR>{ <BR>try <BR>{ <BR>FileStream fileStream = new FileStream(fileName, FileMode.Create); <BR>XmlTextWriter writer = new XmlTextWriter(fileStream, Encoding.UTF8); <BR>List<BookInfo> bookList = GetBookList(); //获取图书列表 <BR>if (bookList != null && bookList.Count > 0) <BR>{ <BR>writer.WriteStartDocument(); <BR>writer.WriteStartElement("bookstore"); //创建父节点 <BR>foreach (BookInfo book in bookList) <BR>{ <BR>writer.WriteStartElement("book"); //创建子节点 <BR>writer.WriteAttributeString("id", book.BookId.ToString()); //添加属性 <BR>writer.WriteAttributeString("category", book.Category); <BR>//图书名称节点 <BR>writer.WriteStartElement("title"); <BR>writer.WriteValue(book.Title); //节点赋值 <BR>writer.WriteEndElement(); <BR>//图书作者节点 <BR>writer.WriteStartElement("author"); <BR>writer.WriteValue(book.Author); <BR>writer.WriteEndElement(); <BR>//出版时间节点 <BR>writer.WriteStartElement("publishDate"); <BR>writer.WriteValue(book.PublishDate.ToString("yyyy-MM-dd")); <BR>writer.WriteEndElement(); <BR>//销售价格节点 <BR>writer.WriteStartElement("price"); <BR>writer.WriteValue(book.Price); <BR>writer.WriteEndElement(); <br><br>writer.WriteEndElement(); //子节点结束 <BR>} <BR>writer.WriteEndElement(); //父节点结束 <BR>} <BR>writer.WriteEndDocument(); <BR>writer.Close(); <BR>fileStream.Close(); <BR>} <BR>catch (Exception e) <BR>{ } <BR>} <BR> <BR>XmlTextWriter类:表示提供快速、非缓存、只进方法的编写器,该方法生成包含 XML 数据的流或文件。 <BR>WriteStartDocument()方法:编写XML的声明。 <BR>WriteEndDocument()方法:关闭任何打开的元素或属性并将编写器重新设置为 Start 状态。 <BR>WriteStartElement(string localName)方法:创建一个节点的开始。 <BR>WriteAttributeString(string localName, string value)方法:给节点添加属性。 <BR>WriteValue(value)方法:给节点赋值。 <br><br>3、其他代码 <br><br>3.1 获取图书列表 <BR><U>复制代码</U> 代码如下: <BR>/// <summary> <BR>/// 获取图书列表 <BR>/// </summary> <BR>/// <returns></returns> <BR>public List<BookInfo> GetBookList() <BR>{ <BR>List<BookInfo> bookList = new List<BookInfo>(); <BR>BookInfo book1 = new BookInfo() { <BR>BookId = 1, <BR>Category = "CHILDREN", <BR>Title = "Harry Potter", <BR>Author = "J K. Rowling", <BR>PublishDate = new DateTime(2005,08,15), <BR>Price = 29.99 <BR>}; <BR>bookList.Add(book1); <BR>BookInfo book2 = new BookInfo() <BR>{ <BR>BookId = 2, <BR>Category = "WEB", <BR>Title = "Learning XML", <BR>Author = "Erik T. Ray", <BR>PublishDate = new DateTime(2003,10,18), <BR>Price = 39.95 <BR>}; <BR>bookList.Add(book2); <BR>return bookList; <BR>} <BR> <BR>3.2 图书信息实体类 <BR><U>复制代码</U> 代码如下: <BR>/// <summary> <BR>/// 图书信息实体类 <BR>/// </summary> <BR>public class BookInfo <BR>{ <BR>public int BookId { set; get; } //图书ID <BR>public string Title { set; get; } //图书名称 <BR>public string Category { set; get; } //图书分类 <BR>public string Author { set; get; } //图书作者 <BR>public DateTime PublishDate { set; get; } //出版时间 <BR>public Double Price { set; get; } //销售价格 <BR>} <BR></div> </div> </div> <div class="share"> </div> <div class="nextinfo"> <p>上一篇:<a href="http://m.paidiu.com/show/1/135604.html" title="C#操作Excel数据增删改查示例">C#操作Excel数据增删改查示例</a></p> <p>下一篇:<a href="http://m.paidiu.com/show/1/135606.html" title="C#获取当前页面的URL示例代码">C#获取当前页面的URL示例代码</a></p> </div> <div class="otherlink"> <h2>最新资源</h2> <ul> <li> <i class="iconfont icon-point"></i> <a class="text-sm" href="/show/1/619123.html" one-link-mark="yes" title="群星《奔赴!万人现场 第2期》[FLAC/分轨][518.87MB]"><span>群星《奔赴!万人现场 第2期》[FLAC/分轨][518.8</span></a> </li> <li> <i class="iconfont icon-point"></i> <a class="text-sm" href="/show/1/619122.html" one-link-mark="yes" title="群星《奇妙浪一夏 (上海迪士尼度假区音乐)》[320K/MP3][43.91MB]"><span>群星《奇妙浪一夏 (上海迪士尼度假区音乐)》[32</span></a> </li> <li> <i class="iconfont icon-point"></i> <a class="text-sm" href="/show/1/619121.html" one-link-mark="yes" title="群星《奇妙浪一夏 (上海迪士尼度假区音乐)》[FLAC/分轨][140.49MB]"><span>群星《奇妙浪一夏 (上海迪士尼度假区音乐)》[FL</span></a> </li> <li> <i class="iconfont icon-point"></i> <a class="text-sm" href="/show/1/619120.html" one-link-mark="yes" title="【古典音乐】詹姆斯·高威《季节》1993[WAV+CUE]"><span>【古典音乐】詹姆斯·高威《季节》1993[WAV+CUE]</span></a> </li> <li> <i class="iconfont icon-point"></i> <a class="text-sm" href="/show/1/619119.html" one-link-mark="yes" title="贝拉芳蒂《卡里普索之王》SACD[WAV+CUE]"><span>贝拉芳蒂《卡里普索之王》SACD[WAV+CUE]</span></a> </li> <li> <i class="iconfont icon-point"></i> <a class="text-sm" href="/show/1/619118.html" one-link-mark="yes" title="小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]"><span>小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]</span></a> </li> <li> <i class="iconfont icon-point"></i> <a class="text-sm" href="/show/1/619117.html" one-link-mark="yes" title="群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]"><span>群星《欢迎来到我身边 电影原声专辑》[320K/MP3</span></a> </li> <li> <i class="iconfont icon-point"></i> <a class="text-sm" href="/show/1/619116.html" one-link-mark="yes" title="群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]"><span>群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨</span></a> </li> <li> <i class="iconfont icon-point"></i> <a class="text-sm" href="/show/1/619115.html" one-link-mark="yes" title="雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]"><span>雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓</span></a> </li> <li> <i class="iconfont icon-point"></i> <a class="text-sm" href="/show/1/619114.html" one-link-mark="yes" title="群星《2024好听新歌42》AI调整音效【WAV分轨】"><span>群星《2024好听新歌42》AI调整音效【WAV分轨】</span></a> </li> </ul> </div> </div> <div class="sidebar"> <div class="cloud"><h2 class="hometitle">标签云</h2><ul><a href="/search.asp?key=1080P高清&m=1">1080P高清<span>(5)</span></a><a href="/search.asp?key=高清电影&m=1">高清电影<span>(5)</span></a><a href="/search.asp?key=百度云盘&m=1">百度云盘<span>(5)</span></a><a href="/search.asp?key=网盘下载&m=1">网盘下载<span>(4)</span></a><a href="/search.asp?key=破解软件&m=1">破解软件<span>(4)</span></a><a href="/search.asp?key=绿色软件&m=1">绿色软件<span>(4)</span></a><a href="/search.asp?key=磁力链接&m=1">磁力链接<span>(3)</span></a><a href="/search.asp?key=强度天梯&m=1">强度天梯<span>(3)</span></a><a href="/search.asp?key=福利资源&m=1">福利资源<span>(2)</span></a><a href="/search.asp?key=王者荣耀&m=1">王者荣耀<span>(2)</span></a><a href="/search.asp?key=黑钻活动&m=1">黑钻活动<span>(2)</span></a><a href="/search.asp?key=WPS会员&m=1">WPS会员<span>(2)</span></a><a href="/search.asp?key=刷图排行&m=1">刷图排行<span>(1)</span></a><a href="/search.asp?key=网盘限速&m=1">网盘限速<span>(1)</span></a><a href="/search.asp?key=鬼灭之刃&m=1">鬼灭之刃<span>(1)</span></a><a href="/search.asp?key=免费韩国漫画&m=1">免费韩国漫画<span>(1)</span></a><a href="/search.asp?key=宫本重做&m=1">宫本重做<span>(1)</span></a><a href="/search.asp?key=免费动漫&m=1">免费动漫<span>(1)</span></a></ul></div> </div> </article> <footer> <p style="font-size: 14px;">友情链接:<a href="http://www.imxmx.com/" title="杰晶网络" target="_blank">杰晶网络</a> <a href="http://www.ddrfans.com/" title="DDR爱好者之家" target="_blank">DDR爱好者之家</a> <a href="http://www.nqxw.com/" title="南强小屋" target="_blank">南强小屋</a> <a href="/" title="黑松山资源网" target="_blank">黑松山资源网</a> <a href="http://www.dyhadc.com/" title="白云城资源网" target="_blank">白云城资源网</a> <a href="/sitemap1.xml">站点地图</a> <a href="/sitemap.xml">SiteMap</a></p> <p>Design by <a href="http://m.paidiu.com">黑松山资源网</a> <a href="/">http://m.paidiu.com</a></p> </footer> <script src="/images/nav.js"></script> <script type="text/javascript"> jQuery.noConflict(); jQuery(function() { var elm = jQuery('#left_flow2'); var startPos = jQuery(elm).offset().top; jQuery.event.add(window, "scroll", function() { var p = jQuery(window).scrollTop(); jQuery(elm).css('position', ((p) > startPos) ? 'fixed' : ''); jQuery(elm).css('top', ((p) > startPos) ? '0' : ''); }); }); </script> </body> </html>