通过C#动态生成图书信息XML文件
程序员文章站
2024-02-27 15:23:03
通过c#动态生成图书信息xml文件(books.xml),文件如下: 复制代码 代码如下:
通过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; } //销售价格
}
复制代码 代码如下:
<?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; } //销售价格
}