Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET XML
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 22:52pm
Edited 25-Mar-13 5:04am
v2
Comments
Monster Maker at 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 at 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 at 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 at 25-Mar-13 4:28am
   
not getting ... :(
Rate this: bad
good
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();
         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.
  Permalink  
Rate this: bad
good
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
        operator_dropdown.Enabled=true;
        operator_dropdown.DataSource=operators;        
}
 
All the operators associated with the name in xml would be added in the dropdown.
 
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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

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);
      }
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.
  Permalink  
v2
Comments
Vimalrashamra at 25-Mar-13 5:52am
   
thanks i will try it out and let u know........
Monster Maker at 25-Mar-13 5:56am
   
I hope i understood your problem right,
 
if you got something out of it...do rate it :)
Vimalrashamra at 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 at 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 at 25-Mar-13 6:34am
   
we can only answer to the problem statement that you stated...
Vimalrashamra at 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
0 Kornfeld Eliyahu Peter 169
1 Zoltán Zörgő 139
2 George Jonsson 135
3 PIEBALDconsult 130
4 OriginalGriff 120
0 OriginalGriff 6,165
1 DamithSL 4,658
2 Maciej Los 4,107
3 Kornfeld Eliyahu Peter 3,649
4 Sergey Alexandrovich Kryukov 3,342


Advertise | Privacy | Mobile
Web04 | 2.8.141220.1 | Last Updated 25 Mar 2013
Copyright © CodeProject, 1999-2014
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