文本框中选择的组合框项目,picturebox XML(XDocument)

时间:2018-05-10 13:57:27

标签: c# xml linq

如何将组合框中所选项目的所有值显示在文本框,图片框中......我的XML文件如下所示:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Animals>
  <Anim id="26">
    <Name>Master Yi</Name>
    <Image>D:\Hobbies\images\MasterYi.png</Image>
    <Description>Description Here</Description>
    <item1>ImageLocation</item1>
  </Anim>
</Animals>

我有这些代码填充我的组合框

void FillComboBox()
    {
        comboBox1.Items.Clear();
        var doc = XDocument.Load(Path.Combine(Environment.CurrentDirectory, "animals.xml"));
        comboBox1.DataSource = doc.Root.Elements()
.Select(e => new { Id = e.Attribute("id").Value, Name = e.Element("Name").Value })
.ToList();
        comboBox1.ValueMember = "Id";
        comboBox1.DisplayMember = "Name";
    }
}

我无法将输出代码与所选项目相关联。我知道我需要在Parse命令中输入id。但是如何实现这一点,我不明白。我需要在我的主窗体(仅视图)和编辑窗体上(用于编辑所选元素并保存) xmlDocument我在XDocument中想要的相同代码

labelName.Text = xmlDoc.DocumentElement.ChildNodes[comboBox1.SelectedIndex].ChildNodes[0].InnerText;
                pictureBoxImg.ImageLocation = xmlDoc.DocumentElement.ChildNodes[comboBox1.SelectedIndex].ChildNodes[1].InnerText;
                textBoxDescription.Text = xmlDoc.DocumentElement.ChildNodes[comboBox1.SelectedIndex].ChildNodes[2].InnerText;
                pictureBoxItem1.ImageLocation = xmlDoc.DocumentElement.ChildNodes[comboBox1.SelectedIndex].ChildNodes[3].InnerText;

1 个答案:

答案 0 :(得分:0)

希望这有帮助。

您需要添加使用System.Xml.Linq;命名空间

public partial class Form1 : Form
{
    private XDocument _xmlDoc;
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        _xmlDoc = XDocument.Load(Path.Combine(Environment.CurrentDirectory, "animals.xml"));
        FillComboBox();
        comboBox1.SelectedIndexChanged += ComboBox1OnSelectedIndexChanged;
    }

    private void ComboBox1OnSelectedIndexChanged(object sender, EventArgs eventArgs)
    {
        var cmb = (ComboBox)sender;
        var selectedIndex = cmb.SelectedIndex;
        var selectedValue = cmb.SelectedValue;

        if (_xmlDoc.Root == null) return;
        var animal = (from el in _xmlDoc.Root.Elements("Anim")
                      where (string)el.Attribute("id") == selectedValue.ToString()
                      select el);

        var imagePath = animal.Select(x => x.Element("Image").Value).FirstOrDefault();
        pictureBox1.ImageLocation = @imagePath;
        txtName.Text = cmb.GetItemText(this.comboBox1.SelectedItem);
        var description = animal.Select(x => x.Element("Description").Value).FirstOrDefault();
        txtDescription.Text = description;
        var itemOne = animal.Select(x => x.Element("item1").Value).FirstOrDefault();
        txtItem.Text = itemOne;
    }

    void FillComboBox()
    {
        comboBox1.Items.Clear();

        comboBox1.DataSource = _xmlDoc.Root.Elements()
            .Select(e => new { Id = e.Attribute("id").Value, Name = e.Element("Name").Value })
            .ToList();
        comboBox1.ValueMember = "Id";
        comboBox1.DisplayMember = "Name";
    }

}

enter image description here