Hi Guys,
I have one XML document and I wanted to show the xml data in listBox.
So what will be the approach for that ?
Please Help...
Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Kirtan PatelRank #292 · 8.6K reputationPosted Jan 12, 2010, 9:00 PM EST
Lalit MRank #1,186 · 1.5K reputationPosted Jan 12, 2010, 8:25 PM EST
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;
}
}
}
more info
--------
http://www.java2s.com/Code/CSharp/GUI-Windows-Form/FillXMLdatatoListBox.htm
Ankit Rathore64 reputationPosted Jan 12, 2010, 8:40 PM EST
<Authors>
<Author>
<FirstName>JohnFirstName>
<LastName>DoeLastName>
Author>
<Author>
<FirstName>JaneFirstName>
<LastName>EodLastName>
Author>
Authors>
now in my listbox I wanted to show First name and last name of Author :\
Birendra Kumar19 reputationPosted Mar 4, 2021, 6:43 AM EST
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
//-------------------------------------
private void button1_Click(object sender, EventArgs e)
{
string path_of_xml = @"C:\Users\Text2.xml";
XmlDocument doc = new XmlDocument();
doc.Load(path_of_xml);
listBox1.Items.Clear();
foreach (XmlNode node in doc.ChildNodes)
{
if (node.Name == "Root")
{
foreach (XmlNode node_of_node in node.ChildNodes)
{
if (node_of_node.Name == "Message")
{
foreach(XmlNode node_node_of_node in node_of_node.ChildNodes )
{
if(node_node_of_node.Name == "Body")
{
string b1 = node_node_of_node["B1"].InnerText;
string b2 = node_node_of_node["B2"].InnerText;
listBox1.Items.Add(b1 + " " + b2 );
}
}
}
}
}
}
}
//----------------------------------------------------------
// -----Text2.xml-----
SIZE 101.6 mm, 25.4 mm
DIRECTION 0,0
SIZE 101.6 mm, 25.4 mm
DIRECTION 0,0
SIZE 101.6 mm, 25.4 mm
DIRECTION 0,0
SIZE 101.6 mm, 25.4 mm
DIRECTION 0,0
SIZE 101.6 mm, 25.4 mm
DIRECTION 0,0