Using Xlinq to populate listbox

Attempting to use XML parent/child node to populate listbox/RichTextBo

Page 1 of 1

2 Replies - 2593 Views - Last Post: 03 May 2010 - 01:15 PM Rate Topic: -----

#1 ExAnimao  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 30-April 10

Using Xlinq to populate listbox

Posted 30 April 2010 - 07:51 AM

C# and Xlinq EXTREME Newbie

-Currently using a 2 dimensional array to populate listbox (so proud of myself)
-Need to switch to using XML instead of hardcoded Array (always trying to improve)
-First dimension text is loaded as a checkbox list into a listbox.
-Second dimension has helper text for selected listboxitem which gets displayed onclick in a RichTextBox
-Same listbox is used by many other functions in the code (have to use code behind)
------------------------------------------------------------------------------
OLD DATA STRUCTURE IS AN ARRAY:
private string[,] listBoxArray = new String[14, 2]{
{"Step 1 Checkbox", Step 1 HelperText} ...
{"Step 14 Checkbox", Step 14 HelperText}
};

OLD CODE (works well)
private System.Windows.Forms.CheckedListBox processCheckedListbox;

//loop through Array and add the First Dimension as list of checkboxes to listbox
int numRows = listBoxArray.GetLength(0);
for (int i = 0; i < numRows; ++i){
processCheckedListbox.Items.Add((object)listBoxArray[i,0]);
}

//call populateRichTextBox when user clicks on item in listbox
processCheckedListbox.SelectedIndexChanged += populateRichTextBox; 

//populate the richtextbox with help text from the Second Dimension of Array
private void populateRichTextBox(object sender, EventArgs e)
{processRichText.Visible = true;
processRichText.Clear();
int msgNum = processCheckedListbox.SelectedIndex;
processRichText.Text += listBoxArray[msgNum,1];
}


*****************************************************************
How do I use XLinq object instead of arrayTo parse the correct step into listbox and add correct helper text to richtext box? This is what I have so far but I am STUCK! To many unknowns for me to find the missing linq.
******************************************************************
NEW DATA STRUCTURE IS LOCATED IN XML FILE:
<Step id="1">
<StepNumber>Step 1 Checkbox</StepNumber>
<StepDetail>Step 1 HelperText</StepDetail>
</Step>
...
<Step id="14">
<StepNumber>Step 14 Checkbox</StepNumber>
<StepDetail>Step 14 HelperText</StepDetail>
</Step>



XElement xmlFile = XElement.Load(@"xmlDATA.xml")

//I am able to produce the above xml tree by using link pad...
var allSteps = from s in xmlFile.Descendants("Step")
select s;
allSteps.Dump();


Now What? How do I populate my listbox with Key (StepNumber) so that
Value(StepDetail) appears in richTextBox

Thanks.

Admin Edit: Please use code tags when posting your code. Code tags are used like so => :code:

Thanks,
PsychoCoder :)

Is This A Good Question/Topic? 0
  • +

Replies To: Using Xlinq to populate listbox

#2 Guest_overtech*


Reputation:

Re: Using Xlinq to populate listbox

Posted 30 April 2010 - 08:18 AM

Quote

***************************************************************************************************************************************
How do I use XLinq object instead of arrayTo parse the correct step into listbox and add correct helper text to richtext box? This is what I have so far but I am STUCK! To many unknowns for me to find the missing linq.
***************************************************************************************************************************************

NEW DATA STRUCTURE IS LOCATED IN XML FILE:

<Step id="1">
<StepNumber>Step 1 Checkbox</StepNumber>
<StepDetail>Step 1 HelperText</StepDetail>
</Step>
...
<Step id="14">
<StepNumber>Step 14 Checkbox</StepNumber>
<StepDetail>Step 14 HelperText</StepDetail>
</Step>

XElement xmlFile = XElement.Load(@"xmlDATA.xml")

//I am able to produce the above xml tree by using link pad...
var allSteps = from s in xmlFile.Descendants("Step")
select s;
allSteps.Dump();


Select the values you want into an anonymous class, then iterate through the values in a foreach.

It would look something like this:

var allSteps = from s in xmlFile.Descendants("Step")
select new {
    _id = (int)s.Attribute("id"),
    _num = (string)s.Element("StepNumber").Value,
    _detail = (string)s.Element("StepDetail").Value
};

ListBox.Items.Clear();
foreach (var r in allSteps)
{
    // Here you can reference the values as r._id, r._num and r._detail
    ListBox.Items.Add(s._id);  // Or whatever
}



However, I would change the XML structure to be something like:

<Step
id='1'
checked='1'
detail='HelperText'
/>
...
<Step
id='14'
checked='0'
detail='HelperText'
/>

Then your query would be:

var allSteps = from s in xmlFile.Descendants("Step")
select new {
    _id = (int)s.Attribute("id"),
    _checked = (bool)s.Attribute("checked").Value.ToString().Equals("1"),
    _detail = (string)s.Attribute("detail").Value
};



- Dave
Was This Post Helpful? 1

#3 ExAnimao  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 30-April 10

Re: Using Xlinq to populate listbox

Posted 03 May 2010 - 01:15 PM

Thanks! Works perfectly now.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1