Read an xml and display it in a listbox
What I want is to show it from a listbox the code that I show you in the image forgive but I can not put it as code because otherwise the tags are not shown. And I want to show the description and class name.
Xml code
<?xml version="1.0" encoding="UTF-8"?>
<furnidata>
<roomitemtypes>
<furnitype id="13" classname="shelves_norja">
<revision>61856</revision>
<defaultdir>0</defaultdir>
<xdim>1</xdim>
<ydim>1</ydim>
<partcolors>
<color>#ffffff</color>
<color>#F7EBBC</color>
</partcolors>
<name>Beige Bookcase</name>
<description>For nic naks and books.</description>
<adurl/>
<offerid>5</offerid>
<buyout>1</buyout>
<rentofferid>-1</rentofferid>
<rentbuyout>0</rentbuyout>
<bc>1</bc>
<excludeddynamic>0</excludeddynamic>
<customparams/>
<specialtype>1</specialtype>
<canstandon>0</canstandon>
<cansiton>0</cansiton>
<canlayon>0</canlayon>
<furniline>iced</furniline>
</furnitype>
<roomitemtypes>
<furnidata>
3 answers
You could help with linq to xml to parse the xml and take the data from the nodes by loading an entity that you can assign to the Datasource of the listbox
LINQ to XML Tutorials with Examples
Basic Queries (LINQ to XML) (C#)
It hurts that you have published the xml as an image and also without giving it a format that facilitates the analysis of the dependency of the tags
What I don't see is that node you have these code you want to list in control
You are going to have to create a class with the corresponding attributes that the XML
using System.Xml.Serialization;
string xml = ("xml");
XmlSerializer xmlConverter = new XmlSerializer(typeof(claseXML));
claseXML datos = (claseXML)xmlConverter.Deserialize(xml);
If all you want is to display an XML file in a ListBox. Use the following code. First you need to read the XML file you need.
//Aquí pones la ubicación del archivo XML
string pathForXML = "rutaXML/archivo.xml";
//Cargas un fileStream con los siguientes parámetros.
FileStream fileStream = new FileStream(pathForXML, FileMode.Open, FileAccess.Read);
//Creas un streamReader el cuál contiene el XML leído.
StreamReader fileRead = new StreamReader(fileStream);
//Ahora recorremos el archivo leído línea por línea y lo agregamos al listBox
string line = "";
while (fileRead.Peek() != -1)
{
line = fileRead.ReadLine();
if (!string.IsNullOrEmpty(line))
{
listBox.Items.Add(line);
}
}
//No podemos olvidar cerrar los Stream.
fileRead.Close();
fileStream.Close();
That's how simple you read an XML file and you have all the corresponding indentation.