开发者

c# xml function to check whether a string is equal to a xml attribute, to add selected combobox item to textbox

开发者 https://www.devze.com 2023-01-01 05:05 出处:网络
i want to check the combobox.selecteditem.tostring() on combobox select in a given xml with several nodes, where each one has an attribute called \"name\"

i want to check the combobox.selecteditem.tostring() on combobox select in a given xml with several nodes, where each one has an attribute called "name"

private void comboBox1_SelectedIndexChanged(o开发者_如何学JAVAbject sender, EventArgs e)
{
    try
    {
        textBox1.AppendText(nameAttributeCheck(comboBox1.SelectedItem.ToString()));
    }
    catch { 
    }
}

private string nameAttributeCheck(string a)
{
    XmlDocument doc = new XmlDocument();
    doc.Load("armor.xml");

    XmlElement root = doc.DocumentElement;
    XmlNodeList items = root.SelectNodes("/items");

    String result = null;

    try
    {
        foreach (XmlNode item in items)
        {
                if (string.Equals(a, item.Attributes["name"].InnerText.ToString()))
                {
                    result += item.Attributes["picture"].InnerText.ToString();
                }
        }

    }
    catch
    {
    }
    return result;

}

each time i try it, nothing happens


ok i got it

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    try
    {
        textBox1.AppendText(nameAttributeCheck(comboBox1.SelectedItem.ToString()));
    }
    catch { 
    }
}

private string nameAttributeCheck(string a)
{
    XmlDocument doc = new XmlDocument();
    doc.Load("armor.xml");

    XmlElement root = doc.DocumentElement;
    XmlNodeList items = root.SelectNodes("/items/item");

    String result = null;

    try
    {
            for (int i = 0; i < items.Count; i++)
            {

                if (string.Equals(a, items[i].Attributes["name"].InnerText.ToString()))
                {
                    result += items[i].Attributes["name"].InnerText.ToString();
                }
            }               
    }
    catch
    {
    }
    return result;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消