0

Bassicly i can load my xml file correctly into a listview but i then want the user to be able to click on the items in the listview and from their display information about that item thats not in the listview but the xml.
So i thought i could load up an xml when the user clicks on the item and somehow fetch that information but it does not display anything on the label. Any help would be appreciated thanx.

XML:

<?xml version = "1.0" encoding="utf-8"?>
<project>
  <test>
    <code>ss</code>
    <name>test</name>
  </test>

  <test>
    <code>ss1</code>
    <name>test1</name>
  </test>
</project>

code:

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Linq;

namespace form
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

    XmlTextReader reader = new XmlTextReader("XMLfile1.xml");
    XmlNodeType type;

    while (reader.Read())
    {
        type = reader.NodeType;
        if (type == XmlNodeType.Element)
        {
            if (reader.Name == "name")
            {
                reader.Read();
                listView1.Items.Add(reader.Value);
            }
        }
    }
    reader.Close();


    }

    private void Form1_Load(object sender, EventArgs e)
    {


    }

    private void listView1_SelectedIndexChanged(object sender, EventArgs e)
    {


        XmlTextReader reader = new XmlTextReader("XMLFile1.xml");
        XmlNodeType type;

        while (reader.Read())
        {
            type = reader.NodeType;

            if (type == XmlNodeType.Element)
            {
                if (reader.Name == "test")
                {
                    reader.Read();
                    codelabel.Text = "s";
                }


            }
        }
    }

    private void groupBox2_Enter(object sender, EventArgs e)
    {

    }
}
}
2
  • What "not working correctly" means? Compile time errors? Run-time errors? Nothing happens? I didn't check the code but in your example the value you read from them XmlTextReader doesn't go anywhere (reader.Read(); codelabel.Text = "s";). Mar 17, 2012 at 16:00
  • it does not display anything when i press an item on the listview, thats why im asking if theirs a way i can get the program to know which item im clicking on and print its subitem on a label
    – Hashey100
    Mar 17, 2012 at 16:04

3 Answers 3

1

Using these extensions,

Write:

protected string GetName(string code)
{
    var item = XElement.Load("XMLfile1.xml").GetEnumerable("test", x =>
            new
            {
                Code = x.Get("code", string.Empty),
                Name = x.Get("name", string.Empty)
            })
            .FirstOrDefault(i => i.Code == code);
    if(null != item)
        return item.Name;
    return "Item not found";
}

This looks up the list from the file, check's each one until it finds the right code and then returns the name value. Then place that value in your control. If I have it wrong that you are searching by name, just exchange the code in the FirstOrDefault line with the return item.Name line.

0

Create a class that mimics the data in your file. Like:

public class Project
{
    public string Code { get; set; }
    public string Name { get; set; }
}

Then, you'll read your XML file into what will end up being a List. Take a look at this tutorial:

Simple XML Parsing

private List<Project> _projects;

You would then add your data from the List to your ListView.

foreach(Project proj in _projects)
{
    listView1.Items.Add(proj.Name); 
}

In your ListView SelectedIndexChanged you can access the proper index of the List to display the proper value to your label.

 codeLabel.Text = _projects[listView1.FocusedItem.Index].Code;
0

The Value property for the test node is empty (it contains only sub-nodes, no text).
I think you should change your code to accumulate the text (with declarations, you can see an example on MSDN) from beginning of test until you read its end tag.

As alternative (if the XML file is not big) you may consider to use LINQ to XML or simply an XmlDocument (it's much more easy to read and you can always have InnerXml of any node). For an example you can look this article on CodeProject.

For a short list of what you can do to parse XML you can read this article (I don't think it's very good but at least it is a...list).

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.