简体   繁体   中英

Can't add items to ListView from my XMLs in WPF

The main problem is this one.

I have 2 XMLs containing information about what my company does. One is considered the template XML where you can find the general information and the other one is the Catalog containing information about each individual equipment, containing a reference to the template XML.

They look like this Catalog XML

<list>
   <A>
      <B>
        <c>reference to template</c>
        <d>info 2</d>
        <e>info 3</e>
        <f>info 4</f>
        <g>
            <h>info5</h>
            <i>info5</i>
        </g>
      </B>
      <B>
        <c>reference to template</c>
        <d>info a</d>
        <e>info s</e>
        <f>info d</f>
        <g>
            <h>infof</h>
            <i>infog</i>
        </g>
      </B>
      <B>
        <c>reference to template</c>
        <d>info h</d>
        <e>info j</e>
        <f>info k</f>
        <g>
            <h>infot</h>
            <i>infoy</i>
        </g>
      </B>
   </A>
</list>

Template

<list>
   <R>
      <S>
        <t>info 7</t>
        <u>info 8</u>
        <v>info 9</v>
        <w>info 10</w>
      </S>
   </R>
</list>

What I need to do is to display all of the equipment catalogued in a listView, which will lits information from both XMLs.

I've tried that and had no succes, all I can display is one equipment, weel it actually isn'y displayed, all that appears is invisible information. I run through both XMLs using this:

xDocument load = xDocument.load("Myxml.xml");
var run = (from x in load.Descendants("A")
           where x.Element("c").Value == comboBox1.SelectedItems.ToString()
           select new
           {
             a = x.Element("d").Valuye.ToString(),
             //here I gather the rest of the information
           }).ToList();
listView.Items.Add(run);
//after that I tried listview.Items.Add(run.a) ... but the code which I use to run through
//ends with FirstorDefault(), instead of ToList() and I try adding all the components manually

The only thing that Appears is an invisible Equipment, which means that when I click on it I can see there's something there, but I just can't see the information.

So I tried adding strings using the same methodology, but got the same result.

Can anyone please tell me where's my mistake? I can't see it.

PS: After I manage to do this, I'm gonna Implement a function, that by double clicking on the information, the client will be able to alter the information. If someone knows where to start with this one, please point me in the right direction

I believe your linq query needs a bit of a touch up, something like:

xDocument load = xDocument.load("Myxml.xml");
var run = (from x in load.Descendants("B")
       where x.Element("c") == comboBox1.SelectedItems.ToString()
       select new
       {
         a = x.Element("d").Valuye.ToString(),
         //here I gather the rest of the information
       }).ToList();

Also, you should try using a for loop on the list and adding the strings one by one

foreach (var item in run)
    listView.Items.Add(item.a);

You can take a look at the different overloads of the Add method on this MSDN page:

http://msdn.microsoft.com/en-us/library/system.windows.forms.listview.listviewitemcollection.aspx

To be honest, I'm not entirely sure what the question is. I'll take a shot at the XDocument query, though.

When doing comparisons, you need to compare the correct types. The comparison from the example won't even compile because there is a comparison of an XElement with a String :

where x.Element("c") == comboBox1.SelectedItems.ToString()

Should be:

where x.Element("c").Value == comboBox1.SelectedItems.ToString()

If the overall goal is to get a list of strings from the query, then take a look at the following:

string match = comboBox1.SelectedItems.ToString();

var doc = XDocument.Load( "MyXml.xml" );

var query = doc.Descendants( "B" )
    .Where( x => x.Element( "c" ).Value == match )
    .Select( x => x.Element( "d" ).Value )
    .ToList();

Notice that the query starts with the "B" element. Starting at the "A" element will result in 0 elements matched in the where clause.

Also, it is easier to break these types of problems down by using additional variables to break down the statement, ie, variables for the matching criteria, for the XDocument, for the query, etc... Even the query could be broken down into further sub-queries if needed.

This should get you started.

If you are using the Detail mode of the list view, you need to add columns to the list and subitems in the items corresponding to each column, else your items will be "invisible".

See the ListView.Columns and ListViewItem.SubItems members for more details.

I found out what was wrong, I needed to add an observable collection, to which I added the content I wanted to put on the viewlist, and then I put the information in the observablecollection in the viewlist.

Thank you all for helping me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM