title: Filling a Listview/Gridview dynamically with XML file
body: <p>good afternoon guys and girls! i am trying to learn c#(WPF) for about 2 weeks now and i'm encountering some problems which google didnt help me to solve so far :/</p>
  
  <p>Lets say i have a random XML file:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;XML&gt;
    &lt;ADRESSE&gt;
      &lt;NAME1&gt;John&lt;/NAME1&gt;
      &lt;NAME2&gt;Doe&lt;/NAME2&gt;
      &lt;STRASSE1&gt;Heystreet&lt;/STRASSE1&gt;
      &lt;STRASSE2&gt;9&lt;/STRASSE2&gt;
      &lt;LAND&gt;AUT&lt;/LAND&gt;
      &lt;PLZ&gt;1050&lt;/PLZ&gt;
      &lt;ORT&gt;Vienna&lt;/ORT&gt;
    &lt;/ADRESSE&gt;
  &lt;/XML&gt;
  </code></pre>
  
  <p>Pretend this XML has like 100 entries.</p>
  
  <p>Now i'll have a simple Listview called "lv1" in my XAML and a button.</p>
  
  <pre><code>        int counter = 0;
          GridView gv1 = new GridView();
          XDocument rndfile = XDocument.Load(@"C:\Users\...\random.xml");
  
          foreach (XElement xele in rndfile.Descendants("ADRESSE")) //#1
          {
  
              GridViewColumn gvc = new GridViewColumn();
              gvc.DisplayMemberBinding = new Binding("Feld"+counter); 
              gvc.Header = xele.Name.LocalName; // #2
              gv1.Columns.Add(gvc); 
              string feldx = string.Format(@"Feld{0}", counter);
             // MessageBox.Show(feldx+"||"+"Feld"+counter); //was for me to test if names are identical
              lv1.Items.Add(new { feldx = xele.Element("Childelement of ADRESSE").Value }); //#3+4
              counter++;
  
          }
          lv1.View = gv1;
  </code></pre>
  
  <p>1 and 3 are my actual problems, whereby 1 and 2 are the same thing i guess.</p>
  
  <p>So basically what i want to do is press the Button, load the XML and create for each Child of ADRESSE a column with the name of the current Child and directly fill it with the XML content.</p>
  
  <p>The problems i am encountering now: #1 the foreach loop only runs for each entry called ADRESSE instead of each child element of it and i just cant figure out how to get the childs of it without breaking any syntax (tried it with Elements() but he doesnt like that in the loop then).. So for now the XML above would only create ONE row instead of 7, because theres only one ADRESSE entry in the file.</p>
  
  <p>For the second problem i want to name the Columns with the Childname of the XML, but due to the problem at #1 it wont work as intended. (or is this thought generally wrong?)</p>
  
  <p>And the third problem is the dynamical filling of the columns. As far as i could see it <code>lv1.Items.Add({...})</code> does not accept the <code>feldx</code> from above, but thinks it is a own name and doesn't fill it correctly then, because there are no columnbindings named feldx. For #4 i need something like <code>feldx = xele.Element(@"{0}", ChildName).Value</code>for the correct content of the column then</p>
  
  <p>I really tried to look and solve this on my own, but all stuff i found on XML or gridviews here or at mycsharp/msdn either was only with static (hardcoded) XAML entrys and bindings or just XML files where you know what entrys there are (so again practically hardcoded). So i just hope my request just isn't too barefaced and someone could enlighten me a little</p>
  
  <p>Edit #1 :  </p>
  
  <pre><code>    var rootele = rndfile.Root.Element("ADRESSE").Elements(); //&lt;-- worked like a charm. My problem was that i tried to fiddle this into the foreach loop itself, which wasn't accepted somehow - so many thanks har07
      int counter = 0;
      foreach (XElement xele in rootele)
      {
          GridViewColumn gvc = new GridViewColumn();
          gvc.DisplayMemberBinding = new Binding("Feld"+counter); 
          gvc.Header = xele.Name.LocalName; 
          gv1.Columns.Add(gvc); 
          lv1.Items.Add(new { feld_x_ = xele.Element("Childelement of ADRESSE").Value }); // &lt;-- for this i am trying to find a solution and post it again, or maybe someone else knows how to add a changing Binding Name  into the .Add()
          counter++;
  
      }
      lv1.View = gv1;
  </code></pre>