Wednesday, March 26, 2008

How to create xml document from database ado.net

I hate to code with xml because i always forgotten the methods, therefore i blog it here so that i could help someone like me. :)

The 0utput lala.xml

<*Library>
<*Book id="1">
<*Name>Programming
<*Author>L.L Yee
<*Book id="2">
<*Name>Mathematics
<*Author>Jason Lee
<*/Library>
You need this code:
using System;
using System.Collections.Generic;
using
System.Text;
using System.Data.SqlClient;
using
System.Configuration;
using System.Xml;
namespace TobyXML
{
class
Program
{
static void Main(string[]
args)
{
ReadCustomerFromDB();
}
public static void
ReadCustomerFromDB()
{
string sql = "Select * from library";
string
output = "";
using (SqlConnection myConnection =
new
SqlConnection(ConfigurationManager.ConnectionStrings["lala"].ConnectionString))
{
myConnection.Open();
SqlCommand
mySqlCommand = new SqlCommand(sql, myConnection);
SqlDataReader datareader =
mySqlCommand.ExecuteReader();
XmlDocument xmlDoc = new
XmlDocument();
xmlDoc.CreateXmlDeclaration("1.0", "UTF-8", null);
XmlNode
nodeLibrary =
xmlDoc.CreateElement("Library");
xmlDoc.AppendChild(nodeLibrary);
while(datareader.Read())
{
XmlNode
nodeBook = xmlDoc.CreateElement("Book");
XmlAttribute attId =
xmlDoc.CreateAttribute("id");
attId.Value =
datareader["bookid"].ToString();
nodeBook.Attributes.Append(attId);
XmlNode
nodeName =
xmlDoc.CreateElement("Name");
nodeName.AppendChild(xmlDoc.CreateTextNode(datareader["bookName"].ToString()));
nodeBook.AppendChild(nodeName);
XmlNode
nodeAuthor =
xmlDoc.CreateElement("Author");
nodeBook.AppendChild(nodeAuthor);
nodeAuthor.AppendChild(xmlDoc.CreateTextNode(datareader["author"].ToString()));
nodeLibrary.AppendChild(nodeBook);
}
datareader.Close();
xmlDoc.Save(@"c:\lala.xml");
}
}
}
}

12 comments:

Anonymous said...

Thanks for the demo. Any chance you can post the lala ConnectionString

Owen Leong said...

I afraid that I lost it. Do you have any problem using this code?

Anonymous said...

Buy Generic Medications Here. Order Generic Medication In own Pharmacy. Buy Pills Central.
[url=http://buypillscentral.com/buy-generic-tamiflu-online.html]Buy Top Quality Viagra, Cialis, Levitra, Tamiflu[/url]. canadian generic drugs. Discount pills pharmacy

Anonymous said...

As a replacement for many years Buy off Viagra Diminish Dispensary has been recognizable all of a add up to leading online apothecary suppliers and customers nearly generic Viagra now the world.

Anonymous said...

Hello!!! carso-owen.blogspot.com is one of the most excellent innovative websites of its kind. I take advantage of reading it every day. carso-owen.blogspot.com rocks!

Anonymous said...

The author of carso-owen.blogspot.com has written an excellent article. You have made your point and there is not much to argue about. It is like the following universal truth that you can not argue with: Artificial grape and banana are delicious. Thanks for the info.

Anonymous said...

http://markonzo.edu http://www.netknowledgenow.com/members/ativan-side-effects.aspx http://blog.bakililar.az/valtrex/ http://www.netknowledgenow.com/members/lorazepam-side-effects.aspx organ

Anonymous said...

carso-owen.blogspot.com is the best. Thank your for this article. I enjoyed it very much.
AAA Toronto Payday Loans 1172 Bay St #101, Toronto, ON M5S 2B4 (416) 477-2817

Anonymous said...

Also that we would do without your brilliant phrase

Anonymous said...

Very usefull
Thanks!

Anonymous said...

Hi
Thanks your article helps me a lot.

Anuj

Anonymous said...

Arejealiw [url=http://bit.ly/elancreditcard]elan credit card[/url] erdsyk