blah blah blah is here! blah blah » Close

up1down
link

Deserialization class is collecting just the first element from the xml.

[XmlElement("typ")]
public string Typ
{
get { return typ; }
set { typ = value; }
}

[XmlElement("mak")]
public string Mak
{
get { return mak; }
set { mak = value; }
}


My xml file.....
<instruz>
<inst>
<Ins>Instrument 1</Ins>
<typ>Instrument 1 Type 1</typ>
<typ>Instrument 1 Type 2</typ> //This option is not displayed
<typ>Instrument 1 Type 3</typ> //This option is not displayed
<mak>Instrument 1 Type 1 Make 1</mak>
<mak>Instrument 1 Type 1 Make 2</mak> //This option is not displayed
<mak>Instrument 1 Type 1 Make 3</mak> //This option is not displayed
<mak>Instrument 1 Type 2 Make 1</mak> //This option is not displayed
<mak>Instrument 1 Type 2 Make 2</mak> //This option is not displayed
<mak>Instrument 1 Type 3 Make 3</mak> //This option is not displayed
</inst>
<inst>
<Ins>Instrument 2</Ins>
<typ>Instrument 2 Type 1</typ>
<typ>Instrument 2 Type 2</typ> //This option is not displayed
<typ>Instrument 2 Type 3</typ> //This option is not displayed
<typ>Instrument 2 Type 4</typ> //This option is not displayed
<mak>Instrument 2 Type 1 Make 1</mak>
<mak>Instrument 2 Type 1 Make 2</mak> //This option is not displayed
<mak>Instrument 2 Type 1 Make 3</mak> //This option is not displayed
<mak>Instrument 2 Type 2 Make 1</mak> //This option is not displayed
<mak>Instrument 2 Type 2 Make 2</mak> //This option is not displayed
<mak>Instrument 2 Type 3 Make 3</mak> //This option is not displayed
<mak>Instrument 2 Type 4 Make 1</mak> //This option is not displayed
<mak>Instrument 2 Type 4 Make 5</mak> //This option is not displayed
</inst>
</instruz>



Im havng three comboBox in my winform. cb1, cb2, cb3.
cb1 displays <Ins> field
cb2 displays <typ> field
cb3 displays <mak> field

But cb2 & cb3 are not fetching all the elements in <typ> / <mak> fields.

last answered one year ago

2 answers

up1down
link

The problem there is that there are multiple 'typ' and 'mak' child nodes in each 'inst' node. You therefore need to use List<string>'s rather than scalar string variables.

This is how I would deserialize your xml file, using a console app:

using System;
using System.Collections.Generic;
using System.IO;
using System.Xml.Serialization;

class Test
{
static void Main()
{
XmlSerializer xs = new XmlSerializer(typeof(Instruments));
Instruments instruments;

using(Stream s = File.OpenRead("gsvirdi.xml"))
{
instruments = (Instruments)xs.Deserialize(s);
}

// check it worked
Console.Clear();

foreach(Instrument inst in instruments.InstList)
{
Console.WriteLine("Ins : {0}", inst.Ins);
foreach(string typ in inst.TypList)
{
Console.WriteLine("typ : {0}", typ);
}
foreach(string mak in inst.MakList)
{
Console.WriteLine("mak : {0}", mak);
}
Console.WriteLine();
}

Console.ReadKey();
}
}

[XmlRoot("instruz")]
public class Instruments
{
[XmlElement("inst")]
public List<Instrument> InstList = new List<Instrument>();
}

public class Instrument
{
string ins;

[XmlElement("Ins")]
public string Ins
{
get { return ins; }
set { ins = value; }
}

[XmlElement("typ")]
public List<string> TypList;

[XmlElement("mak")]
public List<string> MakList;
}


EDIT - see comments below

I'd suggest the following to fit this code into the context of your WF application:
private Instruments instruments;

private void Form1_Load(object sender, EventArgs e)
{
XmlSerializer xs = new XmlSerializer(typeof(Instruments));

using (Stream s = File.OpenRead("gsvirdi.xml"))
{
instruments = (Instruments)xs.Deserialize(s);
}

cb1.DataSource = instruments.InstList;
cb1.DisplayMember = "Ins";

}

// add handler from designer
private void cb1_SelectedIndexChanged(object sender, EventArgs e)
{
int index = cb1.SelectedIndex;
if (index > -1)
{
cb2.DataSource = instruments.InstList[index].TypList;
cb3.DataSource = instruments.InstList[index].MakList;
}

}

gsvirdi
412

and now as I'm displaying these fields in the ComboBox I would need to specify the Datasourse also.... what should I take as the DataSource for these three different combobox??? If I do something like this then also the Datasource is not recognised " foreach (Instrument inst in instruments.InstList) { comboBox1.DataSource = inst.Ins; }"

vulpes
17279

The DataSource for cb1 will be instruments.InstList and the DisplayMember will be "Ins". The DataSource for cb2 and cb3 will change depending on what cb1 is currently showing. Please see my edit.

up0down
link

A script error is always displayed here in my debugging.com's account.....
Line: 88
Char: 27
Error: Expected Identifier, String or Number
Code: 0

Due to this I'm not able to rate the answer, Mark answer as accepted, nor I'm able to add any comment.

Admin... please do something about it.

Feedback