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 !




Read Data from an XML File into a Listbox using C#


Posted Date: 08 May 2008    Resource Type: Code Snippets    Category: Winforms Controls
Author: sriluMember Level: Diamond    
Rating: Points: 10



The following code reads the data in an XML file as items in a ListBox. The code snippet uses a XMLTextReader object to read from an XML file. Each document element in the XML file is then read and added as an item to the ListBox.


XmlTextReader Xreader=new XmlTextReader(HttpContext.Current.Server.MapPath("toolbox.xml"));
Xreader.WhitespaceHandling=WhitespaceHandling.None;
XmlDocument Xdoc=new XmlDocument();
Xdoc.Load(Xreader);
lst_company.Items.Add("LIST OF CONTROLS");
lst_company.Items.Add(" ");
lst_company.Items.Add(" ");
XmlNode Xnode=Xdoc.DocumentElement;
AddText(Xnode,1);


private void AddText(XmlNode Xnode, Int32 intlevel)
{
XmlNode Xnodeworking;
String strindent = new string(' ', 2 * intlevel);
string strvalue = (string)Xnode.Value;
if (strvalue != null)
{
strvalue = " : " + strvalue;
}
if (Xnode.NodeType == XmlNodeType.Element)
{
XmlNamedNodeMap mapattributes = Xnode.Attributes;
foreach (XmlNode xnodattribute in mapattributes)
{
lst_company.Items.Add(strindent + " " + xnodattribute.Name + ":" + xnodattribute.Value);
lst_company.Items.Add(" ");
}
if (Xnode.HasChildNodes)
{
Xnodeworking = Xnode.FirstChild;
while (Xnodeworking != null)
{
AddText(Xnodeworking, intlevel + 1);
Xnodeworking = Xnodeworking.NextSibling;
}
}
}




Responses


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

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Reading xml files  .  Reading xml data into listbox  .  Loading xml data into listbox  .  Listbox control  .  

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: How to move textbox control at runtime
Previous Resource: How to Retrieve Store image from the Database
Return to Discussion Resource Index
Post New Resource
Category: Winforms Controls


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design


Contact Us    Privacy Policy    Terms Of Use