C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » XML »

Building an XML Text Writer document


Posted Date: 27 Nov 2008    Resource Type: Code Snippets    Category: XML
Author: mahendrakiranMember Level: Gold    
Rating: 1 out of 5Points: 5



Here is an example using the XMLTextWriter.
It will build a hierarchical xml text representation of some data,
at the end we will convert it to an XDocument object.

// fetch some data
Models.ProductGroups group = _dataContext.GetGroupById(id);
// declare objects
StringWriter sw = new StringWriter();
XmlTextWriter writer = new XmlTextWriter(sw);

writer.WriteStartElement("Product");
writer.WriteAttributeString("Id", group.id.ToString());
writer.WriteElementString("Title", group.Name);

writer.WriteStartElement("Products");
// View All Products
writer.WriteStartElement("Product");
writer.WriteAttributeString("Id", "0");
writer.WriteAttributeString("DisplayOrder", "0");

writer.WriteElementString("Title", "View All");
// close product
writer.WriteEndElement();
// close console
writer.WriteEndElement();
writer.Close();

// now we will have a string with the xml formatted data in it.
string xml = sw.ToString();

// To convert this xml string to an XDocument do the following
XDocument xmlDoc = XDocument.Parse(xml);




Responses


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

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
XMl Text Writer  .  XML  .  XDocument  .  StreamWriter  .  

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: Recursive TeeNode Comparision
Previous Resource: How to Create new Element ,Text,Attributes into a file
Return to Discussion Resource Index
Post New Resource
Category: XML


Post resources and earn money!
 
Related Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use