Click here to Skip to main content
13,000,135 members (58,974 online)
Rate this:
Please Sign up or sign in to vote.
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 24-Mar-13 21:52pm
Updated 25-Mar-13 4:04am
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();
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);

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 ... :(
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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

XmlDocument doc = new XmlDocument();
         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.
Rate this: bad
Please Sign up or sign in to vote.

Solution 4

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

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
All the operators associated with the name in xml would be added in the dropdown.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

I am telling you in simpler way (through indexes).

XmlDocument doc = new XmlDocument();
      XmlNodeList list = doc.GetElementsByTagName("doc");
      List<String> nameList = new List<string>();
      List<String> operatorList = new List<string>();
      foreach (XmlNode node in list)

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.
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 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...
Vimalrashamra 25-Mar-13 6:57am
Can u give me code to split and bind operator

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.170624.1 | Last Updated 25 Mar 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100