Using C # Dynamic generation of library information XML file _ practical skills

Source: Internet
Author: User
Tags datetime
The Library information XML file (books.xml) is dynamically generated through C #, and the file is as follows:
Copy Code code as follows:

<?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>

Method 1: Use StringBuilder to stitch XML
Copy Code code as follows:

<summary>
Create book information xml
</summary>
public void Createbookxml (string fileName)
{
StringBuilder Xmlresult = new StringBuilder ("<?xml version=\" 1.0\ "encoding=\" utf-8\ "?>");
List<bookinfo> Booklist = Getbooklist (); Get a list of books
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>");
}

Write to File
Try
{
1. Create a file stream
FileStream FileStream = new FileStream (FileName, FileMode.Create);
2. Create the writer
StreamWriter StreamWriter = new StreamWriter (FileStream);
3. Writing content to a file
Streamwriter.writeline (Xmlresult);
4. Turn off the writer
StreamWriter.Close ();
5. Close file stream
Filestream.close ();
}
catch (Exception e)
{ }
}

Method 2: Create XML using the XmlTextWriter class
Copy Code code as follows:

<summary>
Create book information 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 (); Get a list of books
if (Booklist!= null && booklist.count > 0)
{
Writer. WriteStartDocument ();
Writer. WriteStartElement ("bookstore"); Create parent Node
foreach (BookInfo book in Booklist)
{
Writer. WriteStartElement ("book"); Creating child nodes
Writer. WriteAttributeString ("id", book.) Bookid.tostring ()); Add properties
Writer. WriteAttributeString ("category", book. Category);
Book name node
Writer. WriteStartElement ("title");
Writer. WriteValue (book. Title); Node Assignment
Writer. WriteEndElement ();
Book author node
Writer. WriteStartElement ("author");
Writer. WriteValue (book. Author);
Writer. WriteEndElement ();
Publication Time Node
Writer. WriteStartElement ("Publishdate");
Writer. WriteValue (book. Publishdate.tostring ("Yyyy-mm-dd"));
Writer. WriteEndElement ();
Sales Price Node
Writer. WriteStartElement ("Price");
Writer. WriteValue (book. Price);
Writer. WriteEndElement ();

Writer. WriteEndElement (); Child node End
}
Writer. WriteEndElement (); End of parent node
}
Writer. WriteEndDocument ();
Writer. Close ();
Filestream.close ();
}
catch (Exception e)
{ }
}

XmlTextWriter class: Represents a writer that provides fast, cacheable, and forward-only methods that generate streams or files that contain XML data.
WriteStartDocument () Method: writes an XML declaration.
WriteEndDocument () Method: Closes any open elements or attributes and sets the writer back to the start state.
WriteStartElement (String LocalName) method: Creates the beginning of a node.
WriteAttributeString (String localname, String value) method: Adds an attribute to a node.
WriteValue (Value) method: Assigns a value to a node.

3, other code

3.1 Getting a list of books
Copy Code code as follows:

<summary>
Get a list of books
</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 Library Information entity class
Copy Code code as follows:

<summary>
Library Information Entity class
</summary>
public class BookInfo
{
public int BookID {set; get;}//book ID
public string Title {set; get;}//Book name
public string Category {set; get;}//Book category
public string Author {set; get;}//Book author
Public DateTime publishdate {set; get;}//Publication time
Public Double prices {set; get;}//Sales price
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.