1
votes

I'm working on a C# Desktop App and in a module I display the info of a xml file into a listview, I coded my solution with Linq to XML like this

string path = "prueba.xml";

            listView1.Items.Clear();
            listView1.Columns.Add(path, 400);

            XElement doc = XElement.Load(path);

            var result = from persona in doc.Elements("persona")
                         select new{
                             nombre = Convert.ToString(persona.Element("nombre").Value).Trim(),
                             ocupacion = Convert.ToString(persona.Element("ocupacion").Value).Trim()
                         };

 foreach (var persona in result)
            {
                /*ListViewItem item = new ListViewItem(persona.nombre);
                item.SubItems.Add(persona.ocupacion);*/

                ListViewItem nombre = new ListViewItem("<nombre>  " + persona.nombre + "  </nombre>");
                ListViewItem ocupacion = new ListViewItem("<ocupacion>  " + persona.ocupacion + "  </ocupacion>");
                listView1.Items.Add("<persona>");
                listView1.Items.Add(nombre);
                listView1.Items.Add(ocupacion);
                listView1.Items.Add("</persona>");
                listView1.Items.Add("");
            }
             }
        }

and it works very fine, as you can see there are items in the listview that represents the nodes of the xml file, but those items are specific for this xml file

<?xml version="1.0" encoding="utf-8" ?>
<personas>
  <persona>
    <nombre>Pablo el primero</nombre>
    <ocupacion>Programador Cliente-Servidor</ocupacion>
  </persona>
  <persona>
    <nombre>Pablo el segundo</nombre>
    <ocupacion>Programador Web</ocupacion>
  </persona>
</personas>  

as you can see in the C# code, it fits for the xml file above but if I retrieve another xml file with different nodes name like for example

<juego>
<juegos>
<name id="ac"> God of War III </name>
...
</juegos>
</juego>

my code wont show me the nodes <juegos>...</juegos> because it will still display the <person>...</person> nodes because it was created to display only the node <person>...</person> so my question: is there a way to show the information of a xml file into a listview using Linq to XML and at the same time display the info as it is coded in the xml file?

I want to know if I can display this format in teh listview:

<?xml version="1.0" encoding="utf-8" ?>
<personas>
  <persona>
    <nombre>Pablo el primero</nombre>
    <ocupacion>Programador Cliente-Servidor</ocupacion>
  </persona>
  <persona>
    <nombre>Pablo el segundo</nombre>
    <ocupacion>Programador Web</ocupacion>
  </persona>
</personas> 
1
Why do not using TreeView for showing XML data into?Rashed DIP
Do you mean generically display any xml file structure in a list view?DotNetHitMan

1 Answers

0
votes

You don't have to use linq to do it, you can simply query the elements of your document.

listView1.View = System.Windows.Forms.View.Details;
listView1.AutoArrange = false;
listView1.Alignment = ListViewAlignment.Left;
listView1.Items.Clear();
listView1.Columns.Add("XML",400);

    string xml = 
@"<?xml version=""1.0"" encoding=""utf-8"" ?>
<personas>
  <persona>
    <nombre>Pablo el primero</nombre>
    <ocupacion>Programador Cliente-Servidor</ocupacion>
  </persona>
  <persona>
    <nombre>Pablo el segundo</nombre>
    <ocupacion>Programador Web</ocupacion>
  </persona>
</personas>";

        XDocument doc = XDocument.Parse(xml);
        List<ListViewItem> ItemList = new List<ListViewItem>();
        foreach (XElement elem in doc.Elements())
        {
            AddNewItemToList(ItemList, elem);
        }
        listView1.Items.AddRange(ItemList.ToArray());

...

private void AddNewItemToList(List<ListViewItem> ItemList, XElement elem)
    {
        string attributes = "";
        if (elem.HasAttributes)
        {
            foreach (XAttribute attr in elem.Attributes())
            {
                attributes += " " + attr.Name + "=\"" + attr.Value + "\"";
            }
        }

        if (elem.HasElements)
        {
            ItemList.Add(new ListViewItem("<" + elem.Name + attributes + ">"));
            foreach (XElement childElem in elem.Elements())
            {
                AddNewItemToList(ItemList, childElem);
            }
            ItemList.Add(new ListViewItem("</" + elem.Name + ">"));
        }
        else
        {
            ItemList.Add(new ListViewItem("<" + elem.Name + attributes + ">" + elem.Value + "</" + elem.Name + ">"));
        }
    }