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.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Kirtan PatelPosted Jan 13, 2010, 2:00 AM
Birendra KumarPosted Mar 4, 2021, 11:43 AM
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
Ankit RathorePosted Jan 13, 2010, 1:40 AM
<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 :\
Lalit MPosted Jan 13, 2010, 1:25 AM
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