C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




Creating XML Files with Selected Records From a DataSet in C#


Posted Date: 08 May 2008    Resource Type: Code Snippets    Category: XML
Author: sriluMember Level: Diamond    
Rating: Points: 8



Suppose you have an XML file named "books.xml" with the information related to books with bid as the element node. And you want to create another xml file named "mybooks.xml" which contains only the books which i select by entering the id in the textbox TextBox1.Below is the code sample for such a scenario:

   
DataSet ds = new DataSet();
DataSet ds1 = new DataSet();
DataTable dt = new DataTable("books");

ds.ReadXml(Server.MapPath("books.xml"));
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
string bid = Convert.ToString(ds.Tables[0].Rows[i]["bid"].ToString());
if (Convert.ToString(TextBox1.Text.ToLower()) == bid.ToLower())
{
int col = ds.Tables[0].Columns.Count;
ds1.Tables.Add(dt);
dr = ds1.Tables["books"].NewRow();
for (int j = 0; j < col; j++)
{
string colname = ds.Tables[0].Columns[j].ColumnName.ToString();
dt.Columns.Add(colname, typeof(string));
dr[colname] = Convert.ToString(ds.Tables[0].Rows[i][colname].ToString());
}
dt.Rows.Add(dr);
XmlDataDocument doc = new XmlDataDocument(ds1);
XslCompiledTransform transform = new XslCompiledTransform();
transform.Load(Server.MapPath("xsl1.xsl"));
XmlTextWriter writer = new XmlTextWriter(Server.MapPath("mybooks.xml"), Encoding.UTF8);
writer.Formatting = Formatting.Indented;
writer.Indentation = 4;
transform.Transform(doc, null, writer);
}
}




Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Exporting data from a dataset to xml files  .  Creating xml files in c#  .  Creating xml files from a dataset  .  Building xml files in c#  .  Building xml files from a dataset  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: GridView Binding from XML , Using C#
Previous Resource: Creating an XML File from a string containing InnerXML
Return to Discussion Resource Index
Post New Resource
Category: XML


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design


Contact Us    Privacy Policy    Terms Of Use