XML Daten durch eine Suchfunktion Filtern
Hallo zusammen
Bin neu hier und auch noch nicht so erfahren mit WP7 Apps.
Hab ein Problem an dem ich schon seit Stunden herum schraube, finde jedoch einfach keine Lösung.
Es ist so; Ich möchte auf Daten in einem XML-File zugreifen(das klappt auch tiptop)
[FONT=Consolas]public[/FONT][FONT=Consolas] class perkitem[/FONT]
[FONT=Consolas] {[/FONT]
[FONT=Consolas] public string name { get; set; }[/FONT]
[FONT=Consolas] public string pro { get; set; }[/FONT]
[FONT=Consolas] public string ang { get; set; }[/FONT]
[FONT=Consolas] }[/FONT]
[FONT=Consolas] public DownloadStringCompletedEventHandler client_DownloadStrinCompleted { get; set; }[/FONT]
[FONT=Consolas] private void getdata(object sender, RoutedEventArgs e)[/FONT]
[FONT=Consolas] {[/FONT]
[FONT=Consolas] WebClient twitter = new WebClient();[/FONT]
[FONT=Consolas] twitter.DownloadStringCompleted += new DownloadStringCompletedEventHandler(xml_DownloadStringComplete);[/FONT]
[FONT=Consolas] twitter.DownloadStringAsync(new Uri("http://localhost/Perk/xml/perk.xml"));[/FONT]
[FONT=Consolas] }[/FONT]
[FONT=Consolas] void xml_DownloadStringComplete(object sender, DownloadStringCompletedEventArgs e)[/FONT]
[FONT=Consolas] {[/FONT]
[FONT=Consolas] XElement xmlperk = XElement.Parse(e.Result);[/FONT]
[FONT=Consolas] listBox1.ItemsSource = from anzeige in xmlperk.Descendants("AKTION")[/FONT]
[FONT=Consolas] select new perkitem[/FONT]
[FONT=Consolas] {[/FONT]
[FONT=Consolas] name = anzeige.Element("NAME").Value,[/FONT]
[FONT=Consolas] pro = anzeige.Element("PRO").Value,[/FONT]
[FONT=Consolas] ang = anzeige.Element("ANG").Value[/FONT]
[FONT=Consolas] };[/FONT]
[FONT=Consolas] }[/FONT]
Diese Daten werden dann in einer ListBox angezeit(auch das klappt tiptop)
[FONT=Consolas]<[/FONT][FONT=Consolas]Grid[/FONT][FONT=Consolas] x[/FONT][FONT=Consolas]:[/FONT][FONT=Consolas]Name[/FONT][FONT=Consolas]="ContentPanel"[/FONT][FONT=Consolas] Grid.Row[/FONT][FONT=Consolas]="1"[/FONT][FONT=Consolas] Margin[/FONT][FONT=Consolas]="12,0,0,90">[/FONT]
[FONT=Consolas]<[/FONT][FONT=Consolas]ListBox[/FONT][FONT=Consolas] Height[/FONT][FONT=Consolas]="508"[/FONT][FONT=Consolas] HorizontalAlignment[/FONT][FONT=Consolas]="Left"[/FONT][FONT=Consolas] Name[/FONT][FONT=Consolas]="listBox1"[/FONT][FONT=Consolas] VerticalAlignment[/FONT][FONT=Consolas]="Top"[/FONT][FONT=Consolas] Width[/FONT][FONT=Consolas]="468"[/FONT][FONT=Consolas] SelectionChanged[/FONT][FONT=Consolas]="listBox1_SelectionChanged">[/FONT]
[FONT=Consolas]<[/FONT][FONT=Consolas]ListBox.ItemTemplate[/FONT][FONT=Consolas]>[/FONT]
[FONT=Consolas]<[/FONT][FONT=Consolas]DataTemplate[/FONT][FONT=Consolas]>[/FONT]
[FONT=Consolas]<[/FONT][FONT=Consolas]StackPanel[/FONT][FONT=Consolas] Orientation[/FONT][FONT=Consolas]="Horizontal"[/FONT][FONT=Consolas] Width[/FONT][FONT=Consolas]="460">[/FONT]
[FONT=Consolas]<[/FONT][FONT=Consolas]TextBlock[/FONT][FONT=Consolas] Text[/FONT][FONT=Consolas]="{[/FONT][FONT=Consolas]Binding[/FONT][FONT=Consolas] name[/FONT][FONT=Consolas]}" />[/FONT]
[FONT=Consolas]<[/FONT][FONT=Consolas]TextBlock[/FONT][FONT=Consolas] Text[/FONT][FONT=Consolas]=" "/>[/FONT]
[FONT=Consolas]<[/FONT][FONT=Consolas]TextBlock[/FONT][FONT=Consolas] Text[/FONT][FONT=Consolas]="{[/FONT][FONT=Consolas]Binding[/FONT][FONT=Consolas] pro[/FONT][FONT=Consolas]}" />[/FONT]
[FONT=Consolas]<[/FONT][FONT=Consolas]TextBlock[/FONT][FONT=Consolas] Text[/FONT][FONT=Consolas]=" "/>[/FONT]
[FONT=Consolas]<[/FONT][FONT=Consolas]TextBlock[/FONT][FONT=Consolas] Text[/FONT][FONT=Consolas]="{[/FONT][FONT=Consolas]Binding[/FONT][FONT=Consolas] ang[/FONT][FONT=Consolas]}" />[/FONT]
[FONT=Consolas]</[/FONT][FONT=Consolas]StackPanel[/FONT][FONT=Consolas]>[/FONT]
[FONT=Consolas]</[/FONT][FONT=Consolas]DataTemplate[/FONT][FONT=Consolas]>[/FONT]
[FONT=Consolas]</[/FONT][FONT=Consolas]ListBox.ItemTemplate[/FONT][FONT=Consolas]>[/FONT]
[FONT=Consolas]</[/FONT][FONT=Consolas]ListBox[/FONT][FONT=Consolas]>[/FONT]
[FONT=Consolas]</[/FONT][FONT=Consolas]Grid[/FONT][FONT=Consolas]>[/FONT]
[FONT=Consolas]<[/FONT][FONT=Consolas]Button[/FONT][FONT=Consolas] Content[/FONT][FONT=Consolas]="Suchmenü"[/FONT][FONT=Consolas] Grid.Row[/FONT][FONT=Consolas]="1"[/FONT][FONT=Consolas] Height[/FONT][FONT=Consolas]="72"[/FONT][FONT=Consolas] HorizontalAlignment[/FONT][FONT=Consolas]="Left"[/FONT][FONT=Consolas] Margin[/FONT][FONT=Consolas]="12,514,0,0"[/FONT][FONT=Consolas] Name[/FONT][FONT=Consolas]="button1"[/FONT][FONT=Consolas] VerticalAlignment[/FONT][FONT=Consolas]="Top"[/FONT][FONT=Consolas] Width[/FONT][FONT=Consolas]="438"[/FONT][FONT=Consolas] Click[/FONT][FONT=Consolas]="button1_Click" />[/FONT]
[FONT=Consolas]</[/FONT][FONT=Consolas]Grid[/FONT][FONT=Consolas]>[/FONT]
Ich möchte jetzt aber diese Daten durch eine Suchfunktion Filtern, dazu habe ich eine weitere Page erstellt mit drei TextBoxen und einem Button.
Kann mir jemand einen Tipp geben wie ich diese TextBox Parameter verwenden soll.
Ich hatte schon einige Ideen die ich jedoch nicht umsetzen konnte:
-Die XML-Daten zwischenspeichern, mit c# if Statements schreiben und nur diese Daten an die ListBox weitergeben die die Suchbedingungen erfüllen.
-Direkt im XAML Code einen Filter setzen. (??)
Bin ich auf dem Holzweg? Bitte um Hilfe.
Gruss
AW: XML Daten durch eine Suchfunktion Filtern
Hi,
habe mal mit Testdaten gespielt. Folgendes funktioniert zumindest:
Code:
List<Colors> color = new List<Colors>{
new Colors { ID = 1, Name="Color1"},
new Colors { ID = 2, Name = "Color2"},
new Colors { ID = 3, Name = "Color3"},
new Colors { ID = 4, Name = "Color4"}
};
XElement data = new XElement("colors", from c in color where ((int)c.ID > 1)
select new XElement("color",
new XElement("id", c.ID),
new XElement("name", c.Name)));
listBox1.ItemsSource = from anzeige in data.Descendants("color")
select new Colors
{
Name = anzeige.Element("name").Value,
ID = Convert.ToInt16(anzeige.Element("id").Value)
};
Du könntest also mit einer where Klausel die Daten filtern...
Evtl. hilft dir das ja weiter.
AW: XML Daten durch eine Suchfunktion Filtern
Hi loeffel
Zuerst mal danke für Deine Antwort.
Ich habs zwar nicht so wie du gelöst aber du hast mich auf einige Ideen gebracht, dass ist meisstens auch sehr hilfreich:-)
Für die die es interessiert hier meine Lösung, scheint gut zu funktionieren ist aber noch nicht ganz fertig, die if-Bedingung muss natürlich dynamisch sein, halt eine Suchfunktion.
Gruss
Reto
void xml_DownloadStringComplete(object sender, DownloadStringCompletedEventArgs e)
{
List<perkitem> perk = new List<perkitem>();
string name = "";
Int16 pro = 0;
string ang = "";
XDocument perkdoc = XDocument.Parse(e.Result, LoadOptions.None);
foreach (XElement element in perkdoc.Descendants("AKTION"))
{
name = element.Element("NAME").Value;
pro = Convert.ToInt16(element.Element("PRO").Value);
ang = element.Element("ANG").Value;
if (name == "Manor" && pro > 30)
{
perk.Add(new perkitem(name, pro, ang));
}
}
listBox1.ItemsSource = perk;
}