将XML元素与ListBox选定项匹配-C# [英] Matching XML elements to ListBox selected item - C#

查看:76
本文介绍了将XML元素与ListBox选定项匹配-C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要制作它,以便我在列表框中选择的项目在标签中显示其同级元素.当前,我使用XML文件获取<Name>元素,并使用这些<Name>值填充列表框.现在,我想这样做,以便每当在列表框中突出显示<Name>时,它将在表单上的标签中显示与该模块相关的<Code>.

I want to make it so the item I have selected in the list box shows its sibling elements in labels. Currently I used my XML file to get the <Name> element and populate my list box with these <Name> values. Now I want to make it so that whenever the <Name> is highlighted in the list box, it will show the <Code> related to that module in a label on the form.

即如果在列表框"中选择了算法和数据结构",请在表单上的标签中显示其代码",容量",学期"和前提条件".如果选择了其他模块,请对所选模块再次执行该操作

i.e. If in the List Box, Algorithms and Data Structures is selected, show its Code,Capacity,Semester and Prerequisites in labels on the form. If some other module is selected, do that again for the selected module

这是我的XML:

    <?xml version="1.0" encoding="utf-8" ?>
<SoftwareEngineering>
  <Module>
    <Name>Algorithms and Data Structures</Name>
    <Code>3SFE504</Code>
    <Capacity>5</Capacity>
    <Semester>1</Semester>
    <Prerequisite>none</Prerequisite>
  </Module>
  <Module>
    <Name>3D Graphics I</Name>
    <Code>3SFE508</Code>
    <Capacity>5</Capacity>
    <Semester>1</Semester>
    <Prerequisite>none</Prerequisite>
  </Module>
  <Module>
    <Name>Event-Driven Programming</Name>
    <Code>3SFE513</Code>
    <Capacity>10</Capacity>
    <Semester>1</Semester>
    <Prerequisite>none</Prerequisite>
  </Module>
  <Module>
    <Name>Object Oriented Design</Name>
    <Code>3SFE514</Code>
    <Capcity>10</Capcity>
    <Semester>1</Semester>
    <Prerequisite>none</Prerequisite>
  </Module>
  <Module>
    <Name>Requirements Engineering</Name>
    <Code>3SFE516</Code>
    <Capacity>10</Capacity>
    <Semester>1</Semester>
    <Prerequisite>none</Prerequisite>
  </Module>
  <Module>
    <Name>Introduction to AI</Name>
    <Code>3SFE599</Code>
    <Capacity>5</Capacity>
    <Semester>1</Semester>
    <Prerequisite>none</Prerequisite>
  </Module>
  <Module>
    <Name>Java Mobile Application Development</Name>
    <Code>3SFE540</Code>
    <Capacity>5</Capacity>
    <Semester>1</Semester>
    <Prerequisite>3SFE514(corequisite)</Prerequisite>
  </Module>
  <Module>
    <Name>C# .NET Programming</Name>
    <Code>3SFE541</Code>
    <Capacity>5</Capacity>
    <Semester>1</Semester>
    <Prerequisite>3SFE514(corequisite)</Prerequisite>
  </Module>
  <Module>
    <Name>Software Engineering Group Project</Name>
    <Code>3SFE515</Code>
    <Capacity>5</Capacity>
    <Semester>2</Semester>
    <Prerequisite>3SFE514(corequisite)</Prerequisite>
  </Module>
  <Module>
    <Name>Software Engineering</Name>
    <Code>3SFE519</Code>
    <Capacity>10</Capacity>
    <Semester>2</Semester>
    <Prerequisite>none</Prerequisite>
  </Module>
  <Module>
    <Name>Mobile User Interface Development</Name>
    <Code>3SFE542</Code>
    <Capacity>5</Capacity>
    <Semester>2</Semester>
    <Prerequisite>3SFE540</Prerequisite>
  </Module>
  <Module>
    <Name>Interactive Multimedia</Name>
    <Code>3MTS954</Code>
    <Capacity>5</Capacity>
    <Semester>2</Semester>
    <Prerequisite>none</Prerequisite>
  </Module>
  <Module>
    <Name>Concurrent Programming</Name>
    <Code>3SFE555</Code>
    <Capacity>5</Capacity>
    <Semester>2</Semester>
    <Prerequisite>none</Prerequisite>
  </Module>
  <Module>
    <Name>Mobile Gaming</Name>
    <Code>3SFE557</Code>
    <Capacity>10</Capacity>
    <Semester>2</Semester>
    <Prerequisite>none</Prerequisite>
  </Module>
  <Module>
    <Name>Intelligent Systems</Name>
    <Code>3SFE500</Code>
    <Capacity>10</Capacity>
    <Semester>2</Semester>
    <Prerequisite>3SFE599</Prerequisite>
  </Module>
  <Module>
    <Name>3D Graphics II</Name>
    <Code>3SFE501</Code>
    <Capacity>10</Capacity>
    <Semester>2</Semester>
    <Prerequisite>3SFE508</Prerequisite>
  </Module>
</SoftwareEngineering>

这是我尝试实现自己所需的方式:

And here is how I've tried to achieve what I need:

private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            XDocument doc = XDocument.Load(workingDir + @"\Moduleslist.xml");
            var names = doc.Root.Descendants("Module").Elements("Code").Select(b => b.Value);
            var name = doc.Root.Descendants("Module").Elements("Name").Select(a => a.Value);

            if (listBox1.SelectedValue == name)
            {
                labelCodeNumber.Text = names.ToString();
            }

        }

如果有人可以帮助我,我将不胜感激

Please if someone could help me I would appreciate it

推荐答案

我将返回几步,看看您的设计.您将多久直接在代码中访问XML?每次ListBox选定的索引更改时读取文件似乎是一种浪费,更不用说如果文件不可访问,它将引发异常.

I would go back a couple of steps and look at your design. How often will you be accessing the XML directly in your code? Reading a file each time the ListBox selected index changes seems to be a waste, not to mention if the file is not accessible, it will throw an exception.

如果您要访问XML多次,我会考虑创建一个对象来保存XML数据:

If you are going to be accessing the XML numerous times, I would consider is creating an object to hold your XML data:

public class Module
{
    public String Name { get; set; }
    public String Code { get; set; }
    public String Capacity { get; set; }
    public String Semester { get; set; }
    public String Prerequisite { get; set; }
}

然后在阅读XML时创建Module对象的集合

Then create a collection of your Module objects when you read the XML

var modules = (from elem in doc.Root.Descendants("Module")
               select new Module()
               {
                   Name = elem.Element("Name").Value, 
                   Code = elem.Element("Code").Value, 
                   Capacity = elem.Element("Capacity").Value, 
                   Semester = elem.Element("Semester").Value, 
                   Prerequisite = elem.Element("Prerequisite").Value, 
               }).ToDictionary(k=>k.Name,v=>v);

(如果name元素不是唯一的,则您不能做字典,而必须做一个列表)

(if the name elements won't be unique you can't do a dictionary and you'll have to do a list)

然后从该集合中将名称加载到列表框

From that collection, you can then load the names into the listBox

listBox1.Items.AddRange(modules.Keys.ToArray());

然后在您的listBox1_SelectedIndexChanged事件处理程序中,您可以执行以下操作:

And then in your listBox1_SelectedIndexChanged event handler you can do sometihng like:

private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    labelCodeNumber.Text = modules[listBox1.SelectedValue].Code;
}

您还可以考虑将Module对象与ListBox进行数据绑定,但是我对此并不很精明,因此在此我无法提供太多帮助.

You can also consider DataBinding the Module object to the ListBox, but I am not very savvy with it, so I can't offer much help there.

这是将集合放入代码中的方法,以便您可以通过任何方法对其进行访问.

Here's how to put the collection in your code so you can access it any method.

public partial class Form1 : Form
{
    private Dictionary<String, Module> modules;

    public Form1()
    {
        this.modules = LoadXml(XDocument.Load(xmlPath);
    }

    private Dictionary<String, Module> LoadXml(XDocument doc)
    {
        return (from elem in doc.Root.Descendants("Module")
                       select new Module()
                       {
                           Name = elem.Element("Name").Value, 
                           Code = elem.Element("Code").Value, 
                           Capacity = elem.Element("Capacity").Value, 
                           Semester = elem.Element("Semester").Value, 
                           Prerequisite = elem.Element("Prerequisite").Value, 
                       }).ToDictionary(k=>k.Name, v=>v);            
    }


}

这篇关于将XML元素与ListBox选定项匹配-C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆