•   欢迎来到21NN网.
  •   请记住本站网址www.21nn.cn

经由过程C#动态生成图书信息XML文件【C#.Net教程】,C#,生成图书,XML文件

摘要: 经由过程C#动态生成图书信息XML文件(Books.xml),文件以下:<?xmlversion="1.0"encoding="iso-8859-1"?><...
经由过程C#动态生成图书信息XML文件(Books.xml),文件以下:

<?xml version="1.0" encoding="iso-8859-1"?>   
<bookstore>   
  
<book id="1" category="CHILDREN">   
<title>Harry Potter</title>   
<author>J K. Rowling</author>   
<publishDate>2005-08-15</publishDate>   
<price>29.99</price>   
</book>   
  
<book id="2" category="WEB">   
<title>Learning XML</title>   
<author>Erik T. Ray</author>   
<publishDate>2003-10-18</publishDate>   
<price>39.95</price>   
</book>   
  
</bookstore>

要领1:运用StringBuilder拼接XML

/// <summary> 
/// 建立图书信息XML 
/// </summary> 
public void CreateBookXML(string fileName) 
{ 
StringBuilder xmlResult = new StringBuilder("<?xml version=\"1.0\" encoding=\"utf-8\"?>"); 
List<BookInfo> bookList = GetBookList(); //猎取图书列表 
if (bookList != null && bookList.Count > 0) 
{ 
xmlResult.Append("<bookstore>"); 
foreach (BookInfo book in bookList) 
{ 
xmlResult.AppendFormat("<book id=\"{0}\" category=\"{1}\">", book.BookId, book.Category); 
xmlResult.AppendFormat("<title>{0}</title>", book.Title); 
xmlResult.AppendFormat("<author>{0}</author>", book.Author); 
xmlResult.AppendFormat("<publishDate>{0}</publishDate>", book.PublishDate.ToString("yyyy-MM-dd")); 
xmlResult.AppendFormat("<price>{0}</price>", book.Price); 
xmlResult.Append("</book>"); 
} 
xmlResult.Append("</bookstore>"); 
} 

//写入文件 
try 
{ 
//1.建立文件流 
FileStream fileStream = new FileStream(fileName, FileMode.Create); 
//2.建立写入器 
StreamWriter streamWriter = new StreamWriter(fileStream); 
//3.将内容写入文件 
streamWriter.WriteLine(xmlResult); 
//4.封闭写入器 
streamWriter.Close(); 
//5.封闭文件流 
fileStream.Close(); 
} 
catch (Exception e) 
{ } 
}

要领2:运用XmlTextWriter类建立XML

/// <summary> 
/// 建立图书信息XML 
/// </summary> 
/// <param name="fileName"></param> 
public void CreateBookXML(string fileName) 
{ 
try 
{ 
FileStream fileStream = new FileStream(fileName, FileMode.Create); 
XmlTextWriter writer = new XmlTextWriter(fileStream, Encoding.UTF8); 
List<BookInfo> bookList = GetBookList(); //猎取图书列表 
if (bookList != null && bookList.Count > 0) 
{ 
writer.WriteStartDocument(); 
writer.WriteStartElement("bookstore"); //建立父节点 
foreach (BookInfo book in bookList) 
{ 
writer.WriteStartElement("book"); //建立子节点 
writer.WriteAttributeString("id", book.BookId.ToString()); //增加属性 
writer.WriteAttributeString("category", book.Category); 
//图书称号节点 
writer.WriteStartElement("title"); 
writer.WriteValue(book.Title); //节点赋值 
writer.WriteEndElement(); 
//图书作者节点 
writer.WriteStartElement("author"); 
writer.WriteValue(book.Author); 
writer.WriteEndElement(); 
//出书时候节点 
writer.WriteStartElement("publishDate"); 
writer.WriteValue(book.PublishDate.ToString("yyyy-MM-dd")); 
writer.WriteEndElement(); 
//销售价格节点 
writer.WriteStartElement("price"); 
writer.WriteValue(book.Price); 
writer.WriteEndElement(); 

writer.WriteEndElement(); //子节点完毕 
} 
writer.WriteEndElement(); //父节点完毕 
} 
writer.WriteEndDocument(); 
writer.Close(); 
fileStream.Close(); 
} 
catch (Exception e) 
{ } 
}

XmlTextWriter类:示意供应疾速、非缓存、只进要领的编写器,该要领生成包括 XML 数据的流或文件。
WriteStartDocument()要领:编写XML的声明。
WriteEndDocument()要领:封闭任何翻开的元素或属性并将编写重视新设置为 Start 状况。
WriteStartElement(string localName)要领:建立一个节点的最先。
WriteAttributeString(string localName, string value)要领:给节点增加属性。
WriteValue(value)要领:给节点赋值。

3、其他代码

3.1 猎取图书列表

/// <summary>   
/// 猎取图书列表   
/// </summary>   
/// <returns></returns>   
public List<BookInfo> GetBookList()   
{   
List<BookInfo> bookList = new List<BookInfo>();   
BookInfo book1 = new BookInfo() {   
BookId = 1,   
Category = "CHILDREN",   
Title = "Harry Potter",   
Author = "J K. Rowling",   
PublishDate = new DateTime(2005,08,15),   
Price = 29.99   
};   
bookList.Add(book1);   
BookInfo book2 = new BookInfo()   
{   
BookId = 2,   
Category = "WEB",   
Title = "Learning XML",   
Author = "Erik T. Ray",   
PublishDate = new DateTime(2003,10,18),   
Price = 39.95   
};   
bookList.Add(book2);   
return bookList;   
}

3.2 图书信息实体类

/// <summary>   
/// 图书信息实体类   
/// </summary>   
public class BookInfo   
{   
public int BookId { set; get; } //图书ID   
public string Title { set; get; } //图书称号   
public string Category { set; get; } //图书分类   
public string Author { set; get; } //图书作者   
public DateTime PublishDate { set; get; } //出书时候   
public Double Price { set; get; } //销售价格   
}

以上就是经由过程C#动态生成图书信息XML文件的内容,更多相关内容请关注ki4网(www.ki4.cn)!

分享到:

发表评论

评论列表

还没有评论,快来说点什么吧~

公众号二维码

微信公众号