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


i'm struggling make basic c# app gets number of items xml file, shows "title" node in listbox, and, when title selected, displays other nodes of item in textbox. textbox meant allow user edit xml content , save changes.

my problem quite basic think: listbox works fine, textbox isn't updated when new title selected in listbox. guess shouldn't complicated, me - i'm stuck here.

i'm aware questions 1 pop frequently, of them seem me imprecise or overly complicated: i'm (obviously) new c# , keep code simple , transparent 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 textbox part, i've tried everything...

the wfa file contains button loads xml file, listbox, , textbox (perhaps better have text box each xml node)

when xml loaded, put books in list.

suggest use linq2xml (xelement), more convenient 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 form field.

in event handler retrieve book list 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 textbox (set multiline = true) } 

of course, can use several textboxes (or labels).

textboxgenre.text = "genre: " + book.attribute("genre").value; textboxprice.text = "price: " + book.element("price").value; 

and on.


Comments

Popular posts from this blog

php - How to display all orders for a single product showing the most recent first? Woocommerce -

asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

angularjs - How restrict admin panel using in backend laravel and admin panel on angular? -