How to display XML in a listbox and pass it to a textbox in C#?

烈酒焚心 提交于 2019-12-13 08:36:34

问题


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.

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.

My XML sample:

<?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>

The cs file

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...

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)


回答1:


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

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);
    }
}

Where List<XElement> bookList is a form field.

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;

And so on.



来源:https://stackoverflow.com/questions/40569520/how-to-display-xml-in-a-listbox-and-pass-it-to-a-textbox-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!