Click here to Skip to main content
15,888,579 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have Xml file


I want to read this and add the name in first dropdown ,
on selection of NameDropdown .... i want to split the operators by , and bind them in another dropdown .
and if type is string then show a text box and if id then user can enter only numbers in that .

i tried to read it too and getting names but stucked on the how i split the operator and bind them in dropdown
Posted
Updated 25-Mar-13 4:04am
v2
Comments
Monster Maker 25-Mar-13 3:58am    
as you get Operators property value by parsing,

can't you just split the value into and array(through String.split(',')),

and bind that array to the dropdown.
Vimalrashamra 25-Mar-13 4:02am    
XmlDocument doc = new XmlDocument();
doc.LoadXml(xmlContent);
XmlNodeList list = doc.GetElementsByTagName("doc");
foreach (XmlNode node in list)
{
ListItem lItemName = new ListItem(node.Attributes["Name"].Value);
ListItem lItemOperator = new ListItem(node.Attributes["Operators"].Value);
ListItem lItemType = new ListItem(node.Attributes["Type"].Value);
DropDownListFilterByName.Items.Add(lItemName);
DropDownListOperator.Items.Add(lItemOperator);
}

This is my code and i can split but how i bind it on filterByName_selectedindexchange
Monster Maker 25-Mar-13 4:12am    
In that case, you want something that associates the name to the operator,
may be by index. Or you can use a data structure with key-value pair,
where key is your name and value is the operator.
and bind that operator value(after splitting) to the dropdownlist at selectedindexchange event.
Vimalrashamra 25-Mar-13 4:28am    
not getting ... :(

This is how you can do this through key(name)-value(operator) pair.

C#
XmlDocument doc = new XmlDocument();
         doc.LoadXml("xmlContent");
         XmlNodeList list = doc.GetElementsByTagName("doc");
         System.Collections.Generic.Dictionary<String, String> kv = new System.Collections.Generic.Dictionary<String, String>();
         foreach (XmlNode node in list)
         {
             kv.Add(node.Attributes["Name"].Value, node.Attributes["Operators"].Value);
         }
         string noValue = "noValue";
         string operatorString=kv.TryGetValue("ComapnyName",out noValue).ToString();


Now the operator string would have the strings that you defined in xml,
on selectedindexchange event you can get the value of selected name,
through that TryGetValue() you can get string of associated operators with that name,
split the name and bind it to the operator's drop-down.

noValue is the default value(in case it doesn't find the key), and you have to pass it with out keyword.
 
Share this answer
 
Well i knew about that part, but left it on you.

Here is the complete solution,

//Terms used in the code

name_dropdown= dropdown of the names

operator_dropdown=dropdown of the operators

kv=Key-Value pair Dictionary data structure. Having key=names and value=operators

noValue=default string value when given name is not found

//Code for spliting the operator and binding them in operator_dropdown on selection
of a value in name_dropdown

C#
public void name_dropdown_SelectedIndexChange(object sender, EventArgs e)
{
        String selected_name=name_dropdown.Text;
        //The key value pair data structure should be create global so that you can use
        it here
        String noValue="noValue";
        String operator_string=kv.TryGetValue(selected_name, out noValue);
        String[] operators=operator_string.split(',');
       
        //Enabled only once the user selects the name
        operator_dropdown.Enabled=true;
        operator_dropdown.DataSource=operators;        
}

All the operators associated with the name in xml would be added in the dropdown.
 
Share this answer
 
v2
I am telling you in simpler way (through indexes).


C#
XmlDocument doc = new XmlDocument();
      doc.LoadXml(xmlContent);
      XmlNodeList list = doc.GetElementsByTagName("doc");
      List<String> nameList = new List<string>();
      List<String> operatorList = new List<string>();
      foreach (XmlNode node in list)
      {
          nameList.Add(node.Attributes["Name"].Value);
          operatorList.Add(node.Attributes["Operators"].Value);
      }

C#
String[] names = nameList.ToArray();
  String[] operators = operatorList.ToArray();



Now you can bind the names array to the dropdown,
and on selectedindexchange you will get the index.

The operator string would be placed in same index in operators array,
split that string, and bind it to your operator dropdown.
 
Share this answer
 
v2
Comments
Vimalrashamra 25-Mar-13 5:52am    
thanks i will try it out and let u know........
Monster Maker 25-Mar-13 5:56am    
I hope i understood your problem right,

if you got something out of it...do rate it :)
Vimalrashamra 25-Mar-13 6:11am    
The operator string would be placed in same index in operators array,
split that string, and bind it to your operator dropdown.

not working for me
Monster Maker 25-Mar-13 6:20am    
1st thing is prefer 2nd solution,

see, when you use nameList.toArray(), it would provide the names-indexes order in which you inserted them. Like if you inserted companyName and then companyId it would provide them the index 0 and 1 respectively and return the string[].

same is with operatorsList.toArray(), the "companyNameOperators" is inserted in operatorsList and then "companyIdOperators" , so their indexes would be 0 and 1 respectively,

now names array at 0 index has "company name"
and operators array at 0(same index) index has "company operators"

so for getting operators of perticular name, you should get its index from names[] and
you will get corresponding operators with same index from operators array.

Have i made it clear?
Monster Maker 25-Mar-13 6:34am    
we can only answer to the problem statement that you stated...

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