views:

114

answers:

1

I've posted the same question here and I've also got couple of good answers as well. While I was trying the same answers, I was getting compilation errors. Later I got to know that we are using .NET 2.0 and our existing application has no references to LINQ files.

After searching in SO, i tried to figured out partly:

public partial class Item
    {
        public object CHK { get; set; }
        public int SEL { get; set; }
        public string VALUE { get; set; }
    }

Parsing:

        XmlDocument doc = new XmlDocument();
        doc.LoadXml("<LISTBOX_ST>
             <item><CHK></CHK><SEL>00001</SEL><VALUE>val01</VALUE></item>
             <item><CHK></CHK><SEL>00002</SEL><VALUE>val02</VALUE></item>
             <item><CHK></CHK><SEL>00003</SEL><VALUE>val03</VALUE></item>
             <item><CHK></CHK><SEL>00004</SEL><VALUE>val04</VALUE></item>
             <item><CHK></CHK><SEL>00005</SEL><VALUE>val05</VALUE></item>
                   </LISTBOX_ST>");            
       List<Item> _lbList = new List<Item>();
       foreach (XmlNode node in doc.DocumentElement.ChildNodes)
        {
            string text = node.InnerText; //or loop through its children as well
            //HOW - TO - POPULATE THE ITEM OBJECT ??????
        }
       listBox1.DataSource = _lbList;
       listBox1.DisplayMember = "VALUE";
       listBox1.ValueMember = "SEL";

How to read two child nodes - SEL and VALUE of node and populate the same in the new Item DTO??

+2  A: 

You could do it just like this:

    XmlDocument doc = new XmlDocument();
    doc.LoadXml(@"<LISTBOX_ST>
     <item><CHK></CHK><SEL>00001</SEL><VALUE>val01</VALUE></item>
     <item><CHK></CHK><SEL>00002</SEL><VALUE>val02</VALUE></item>
     <item><CHK></CHK><SEL>00003</SEL><VALUE>val03</VALUE></item>
     <item><CHK></CHK><SEL>00004</SEL><VALUE>val04</VALUE></item>
     <item><CHK></CHK><SEL>00005</SEL><VALUE>val05</VALUE></item>
     </LISTBOX_ST>");            

    List<Item> _lbList = new List<Item>();
   foreach (XmlNode node in doc.DocumentElement.ChildNodes)
    {
        string chk = node.ChildNodes[0].InnerText;
        int sel = int.Parse(node.ChildNodes[1].InnerText);
        string value = node.ChildNodes[2].InnerText;

        Item item = new Item();
        item.CHK = chk;
        item.VALUE = value;
        item.SEL = sel;

        _lbList.Add(item);
    }
   listBox1.DataSource = _lbList;
   listBox1.DisplayMember = "VALUE";
   listBox1.ValueMember = "SEL";
Alex