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 :/
Lets say i have a random XML file:
<?xml version="1.0" encoding="UTF-8"?>
<XML>
<ADRESSE>
<NAME1>John</NAME1>
<NAME2>Doe</NAME2>
<STRASSE1>Heystreet</STRASSE1>
<STRASSE2>9</STRASSE2>
<LAND>AUT</LAND>
<PLZ>1050</PLZ>
<ORT>Vienna</ORT>
</ADRESSE>
</XML>
Pretend this XML has like 100 entries.
Now i'll have a simple Listview called "lv1" in my XAML and a button.
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;
1 and 3 are my actual problems, whereby 1 and 2 are the same thing i guess.
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.
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.
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?)
And the third problem is the dynamical filling of the columns. As far as i could see it lv1.Items.Add({...})
does not accept the feldx
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 feldx = xele.Element(@"{0}", ChildName).Value
for the correct content of the column then
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
Edit #1 :
var rootele = rndfile.Root.Element("ADRESSE").Elements(); //<-- 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 }); // <-- 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;