Click here to Skip to main content
15,887,945 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
Hi ,
I have a checkboxlist with 38 listitems,table columns are "PerfCounter" "ID"
The way iam retrieving from the database is:
C#
DataTextField="PerfCounter"
DataValueField="ID"

I placed a lable in the form & if you select any listbox it will displays the corresponding value and the code behind is:

C#
protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
{
    Label1.Text = string.Empty;

    foreach (ListItem listitem in CheckBoxList1.Items)
    {
        if (listitem.Selected)
            Label1.Text += listitem.Value + "<br />";
    }
}

Till now it is working fine.
Issue:I just want to build a string with the selected listitems like
XML
<Root><PC ID="1"/><PC ID="6"/><PC ID="12"/><PC ID="28"/></Root>

so that i can use that string as a parametr to my storedprocedure to design reports. Ex:
EXEC [Report].[usp_GetPermonCounterData] @ServerID = 543, @xmlDocument = '<Root><PC ID="1"/><PC ID="6"/><PC ID="12"/><PC ID="28"/></Root>'
Posted
Updated 5-Jun-12 5:20am
v4

If you need to handle xml as string you could use something like this:
C#
protected string GetAsXMLString(CheckBoxList itemsource)
{
    StringBuilder sb = new StringBuilder("<root>");

    foreach (ListItem listitem in itemsource.Items)
    {
        if (listitem.Selected)
            sb.Append(String.Format("<PC ID=""{0}""/>", listitem.Value));
    }
    sb.Append("</root>")
    return sb.ToString();
}
 
Share this answer
 
v4
Comments
myansweris 5-Jun-12 10:26am    
Thankyou...
Zoltán Zörgő 5-Jun-12 10:27am    
You are welcome...
Sandeep Mewara 5-Jun-12 10:31am    
my 5!
VJ Reddy 5-Jun-12 11:27am    
Good answer. 5!
Shahin Khorshidnia 5-Jun-12 12:46pm    
Perfect + FiVe
The Solution 1 given by Zoltán Zörgő is good.

If you want to use LINQ then the following query can be used to get parameter as an XML string
C#
string paramValue = CheckBoxList1.Items.OfType<ListItem>().Where (li =>
    li.Selected).Aggregate ("<Root>",(string agg, ListItem li) =>
    agg += string.Format(@"<PC ID=""{0}""/>",li.Value)) + "</Root>";

//Sample paramValue
//<Root><PC ID="1"/><PC ID="6"/></Root>
 
Share this answer
 
v4
Comments
Shahin Khorshidnia 5-Jun-12 12:48pm    
Good Complementary My +1 (joking) a flawless FIVE :D
VJ Reddy 5-Jun-12 12:59pm    
Thank you very much, Shahin :D
sjelen 5-Jun-12 13:03pm    
Oh, the endless possibilities of LINQ extensions :) +5
VJ Reddy 5-Jun-12 13:04pm    
Thank you, sjelen :)
Maciej Los 5-Jun-12 14:44pm    
Good answer, my 5!
Use xlinq:

C#
var x = new XElement("Root",
    new XElement("PC", new XAttribute("ID", "1")),
    new XElement("PC", new XAttribute("ID", "6")),
    new XElement("PC", new XAttribute("ID", "12")),
    new XElement("PC", new XAttribute("ID", "28")));
//or
x.Add(new XElement("PC", new XAttribute("ID", "36")));
Console.WriteLine(x.ToString());
 
Share this answer
 
Comments
VJ Reddy 5-Jun-12 19:47pm    
The OP wants to build the XML string from the selected items of CheckBoxList.
Here the literal values are used in building the XML string.
Even if only literal values are required by OP then there is no need of XElement as OP does not want to manipulate with XElement. Finally only string is required in which case
"<root><PC ID="1"/><PC ID="6"/><PC ID="12"/><PC ID="28"/>" will do.
Hence I have not voted.
How it is useful for the above question, can you please elaborate.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900