如何匹配XML元素 - C# [英] How to match elements in XML - C#

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

问题描述

我用一种形式来显示XML元素。每当我一个模块上单击列表框我想显示其<内部;代码>例如值,在窗体上的标签。我遇到的问题是,我已经做了它的方式是,XML被读取一次全部,我不能够,或者不知道怎么来的,我的搜索XML文件中。所以,我想我只是需要一种方法在XML中进行搜索,找到<代码方式> 元素



编辑:这里是我多远,试图显示<代码> 时,它匹配的标签元素的<名称> 在列表框中元素:

 私人无效listBox1_SelectedIndexChanged(对象发件人,EventArgs五)
{

变种文件= XDocument.Load(workingDir + @\ModulesList.xml);

变种代码=从document.Descendants(姓名)
d其中d.Value == listBox1.SelectedItem.ToString()
选择d.Parent.Element( 守则)的价值。

labelCodeNumber.Text = code.ToString();

}



但问题是,labelCodeNumber不显示的代码值,它会显示一些奇怪的东西的集合,而不是。此外,我不知道如果我做了正确的输出即 labelCodeNumber.Text = code.ToString();



和这里是XML:<?/ p>

 < XML版本=1.0编码=UTF-8> ; 
< SoftwareEngineering>
<&模块GT;
<名称>算法与数据结构< /名称>
<代码和GT; 3SFE504< /代码>
<容量和GT; 5℃/容量>
<&学期大于1< /学期>
<&先决条件GT;没有< /先决条件>
< /模块>
<&模块GT;
<名称> 3D图形I< /名称>
<代码和GT; 3SFE508< /代码>
<容量和GT; 5℃/容量>
<&学期大于1< /学期>
<&先决条件GT;没有< /先决条件>
< /模块>
<&模块GT;
<名称>事件驱动编程< /名称>
<代码和GT; 3SFE513< /代码>
<容量大于10< /容量>
<&学期大于1< /学期>
<&先决条件GT;没有< /先决条件>
< /模块>
<&模块GT;
<名称>面向对象设计LT; /名称>
<代码和GT; 3SFE514< /代码>
< Capcity大于10< / Capcity>
<&学期大于1< /学期>
<&先决条件GT;没有< /先决条件>
< /模块>
<&模块GT;
<名称>要求工程LT; /名称>
<代码和GT; 3SFE516< /代码>
<容量大于10< /容量>
<&学期大于1< /学期>
<&先决条件GT;没有< /先决条件>
< /模块>
<&模块GT;
<名称>介绍AI< /名称>
<代码和GT; 3SFE599< /代码>
<容量和GT; 5℃/容量>
<&学期大于1< /学期>
<&先决条件GT;没有< /先决条件>
< /模块>
<&模块GT;
<名称> Java的移动应用开发和LT; /名称>
<代码和GT; 3SFE540< /代码>
<容量和GT; 5℃/容量>
<&学期大于1< /学期>
<&先决条件GT; 3SFE514(并存)LT; /先决条件>
< /模块>
<&模块GT;
<名称> C#.NET编程< /名称>
<代码和GT; 3SFE541< /代码>
<容量和GT; 5℃/容量>
<&学期大于1< /学期>
<&先决条件GT; 3SFE514(并存)LT; /先决条件>
< /模块>
<&模块GT;
<名称>软件工程组项目与LT; /名称>
<代码和GT; 3SFE515< /代码>
<容量和GT; 5℃/容量>
<&学期GT; 2'; /学期>
<&先决条件GT; 3SFE514(并存)LT; /先决条件>
< /模块>
<&模块GT;
<名称>软件工程,LT; /名称>
<代码和GT; 3SFE519< /代码>
<容量大于10< /容量>
<&学期GT; 2'; /学期>
<&先决条件GT;没有< /先决条件>
< /模块>
<&模块GT;
<名称>移动用户界面开发和LT; /名称>
<代码和GT; 3SFE542< /代码>
<容量和GT; 5℃/容量>
<&学期GT; 2'; /学期>
<&先决条件GT; 3SFE540< /先决条件>
< /模块>
<&模块GT;
<名称>互动多媒体< /名称>
<代码和GT; 3MTS954< /代码>
<容量和GT; 5℃/容量>
<&学期GT; 2'; /学期>
<&先决条件GT;没有< /先决条件>
< /模块>
<&模块GT;
<名称>并发编程< /名称>
<代码和GT; 3SFE555< /代码>
<容量和GT; 5℃/容量>
<&学期GT; 2'; /学期>
<&先决条件GT;没有< /先决条件>
< /模块>
<&模块GT;
<名称>手机游戏< /名称>
<代码和GT; 3SFE557< /代码>
<容量大于10< /容量>
<&学期GT; 2'; /学期>
<&先决条件GT;没有< /先决条件>
< /模块>
<&模块GT;
<名称>智能系统与LT; /名称>
<代码和GT; 3SFE500< /代码>
<容量大于10< /容量>
<&学期GT; 2'; /学期>
<&先决条件GT; 3SFE599< /先决条件>
< /模块>
<&模块GT;
<名称> 3D图形II蛋白酶/名称>
<代码和GT; 3SFE501< /代码>
<容量大于10< /容量>
<&学期GT; 2'; /学期>
<&先决条件GT; 3SFE508< /先决条件>
< /模块>
< / SoftwareEngineering>



最后编辑:
我想通了!由于马尔辛和psubsee的帮助和贡献。下面这段代码放在了listbox1_selectedindexchanged方法

  VAR代码=由D在document.Descendants(姓名)
其中,d.Value ==(字符串)listBox1.SelectedItem
选择d.Parent.Element(守则)的价值。

的foreach(代码变种项)
{
labelCodeNumber.Text = item.ToString();
}


解决方案

您应该改变你的观念,用它代替的XmlReader的XDocument:

  VAR文件= XDocument.Load(workingDir + @\ModulesList.xml); 

要填补ListBox中使用类似的东西:

  VAR项目=从document.Descendants(姓名)
D选择d.Value;

的foreach(在项目VAR项目)
listBox1.Items.Add(项目);



然后找到<代码> 从所选元素可以做出那种的LINQ到XML查询:

  VAR代码=从document.Descendants D(名称)
,其中d.Value == SelectedName
选择d.Parent.Element(守则)。价值


I'm using a form to display XML elements. Every time I click on a module inside a listbox I want to display its <Code> value for instance, in a label on the form. The problem I'm having is that the way I've done it is that the XML is being read all at once, and I'm not able to, or don't know how to, search inside my XML file. So I think I just need a way to search in XML to find the <Code> element.

EDIT: Here is how far I am with trying to display the <Code> element in the label when it matches the <Name> element in the listbox:

private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{

    var document = XDocument.Load(workingDir + @"\ModulesList.xml");

    var code = from d in document.Descendants("Name")
               where d.Value == listBox1.SelectedItem.ToString()
               select d.Parent.Element("Code").Value;

    labelCodeNumber.Text = code.ToString();

}

But the problem is that the labelCodeNumber doesn't display the value of code, it displays some weird collections stuff instead. Also I'm not sure if I am doing the output right i.e. labelCodeNumber.Text = code.ToString();

And here is the 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>

FINAL EDIT: I figured it out! Thanks to Marcin and psubsee for their help and contribution. This code below goes in the listbox1_selectedindexchanged method

var code = from d in document.Descendants("Name")
           where d.Value == (String) listBox1.SelectedItem
           select d.Parent.Element("Code").Value;

foreach (var item in code)
{
    labelCodeNumber.Text = item.ToString();
}

解决方案

You should change your concept and use XDocument instead of XmlReader:

var document = XDocument.Load(workingDir + @"\ModulesList.xml");

To fill up the listbox use something like that:

var items = From d in document.Descendants("Name")
            select d.Value;

foreach(var item in items)
    listBox1.Items.Add(item);

And then to find <code> from selected element you can make that kind of Linq to XML query:

var code = From d in document.Descendants("Name")
           where d.Value == SelectedName
           select d.Parent.Element("Code").Value

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

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