如何在列表框中显示XML并将其传递给C#中的文本框? [英] How to display XML in a listbox and pass it to a textbox in C#?

查看:193
本文介绍了如何在列表框中显示XML并将其传递给C#中的文本框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力制作一个基本的C#应用​​程序,该应用程序从XML文件中获取大量项目,并在列表框中显示标题"节点,并在选择标题时在其中显示项目的其他节点.一个文本框.该文本框旨在允许用户编辑XML内容并保存更改.

I'm struggling to make a basic C# app that gets a number of items from an XML file, shows the "title" node in a listbox, and, when a title is selected, displays the other nodes of the item in a textbox. The textbox is meant to allow the user to edit the XML content and save the changes.

我认为我的问题很基本:列表框工作正常,但是在列表框中选择新标题时,文本框不会更新.我想它应该不会太复杂,但对我来说却是-我真的被困在这里.

My problem is quite basic I think: The listbox works fine, but the textbox isn't updated when a new title is selected in the listbox. I guess it shouldn't be too complicated, but to me it is - I'm really stuck here.

我知道类似这样的问题经常出现,但是在我看来,其中大多数问题都不精确或过于复杂:我(显然)是C#的新手,真的很想保持代码的简单和透明.可能.

I'm aware that questions like this one pop up frequently, but most of them seem to me to be imprecise or overly complicated: I'm (obviously) new to C# and really like to keep code as simple and transparent as possible.

我的XML示例:

<?xml version='1.0'?>
  <book genre="autobiography" publicationdate="1981" ISBN="1-861003-11-0">
    <title>The Autobiography of Benjamin Franklin</title>
    <author>
      <first-name>Benjamin</first-name>
      <last-name>Franklin</last-name>
    </author>
    <price>8.99</price>
  </book>
  <book genre="novel" publicationdate="1967" ISBN="0-201-63361-2">
    <title>The Confidence Man</title>
    <author>
      <first-name>Herman</first-name>
      <last-name>Melville</last-name>
    </author>
    <price>11.99</price>
  </book>
  <book genre="philosophy" publicationdate="1991" ISBN="1-861001-57-6">
    <title>The Gorgias</title>
    <author>
      <name>Plato</name>
    </author>
    <price>9.99</price>
  </book>
</bookstore>

cs文件

private void btnLireXML_Click(object sender, EventArgs e)
{
    XmlDocument xDox = new XmlDocument();
    xDoc.Load(books.xml);

    XmlNodeList lst = xDoc.GetElementsByTagName("title");
    foreach (XmlNode n in lst)
    {
        listBox1.Items.Add(n.InnerText); 
    }   

}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    textBox1.Text = listBox1.SelectedItem.ToString();
}

在这里,在文本框部分,我已经尝试了几乎所有内容...

Here, in the textbox part, I've tried almost everything...

WFA文件包含一个加载XML文件的按钮,一个列表框和一个文本框(也许最好为每个XML节点都有一个文本框)

The WFA file contains a button that loads the XML file, a listbox, and a textbox (perhaps it would be better to have a text box for each XML node)

推荐答案

加载xml时,将书籍放在列表中.

When xml is loaded, put the books in the list.

建议使用linq2xml(XElement),因为它比旧版XmlDocument更方便.

Suggest to use linq2xml (XElement), as it is a more convenient than the legacy XmlDocument.

private void ButtonLoad_Click(object sender, EventArgs e)
{
    var xml = XElement.Load("books.xml");

    bookList = xml.Elements("book").ToList();

    foreach (var book in bookList)
    {
        string title = book.Element("title").Value;
        listBox.Items.Add(title);
    }
}

List<XElement> bookList是表单字段.

在事件处理程序中,按索引从列表中检索图书.

In the event handler retrieve the book from the list by index.

private void ListBox_SelectedIndexChanged(object sender, EventArgs e)
{
    var book = bookList[listBox.SelectedIndex];

    textBox.Text =
        "Genre: " + book.Attribute("genre").Value + Environment.NewLine +
        "Price: " + book.Element("price").Value;
    // Put other values to textbox (set Multiline = true)
}

当然,您可以使用多个文本框(或标签).

Of course, you can use several textboxes (or labels).

textBoxGenre.Text = "Genre: " + book.Attribute("genre").Value;
textBoxPrice.Text = "Price: " + book.Element("price").Value;

以此类推.

这篇关于如何在列表框中显示XML并将其传递给C#中的文本框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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