question

Mantra-1363 avatar image
0 Votes"
Mantra-1363 asked karenpayneoregon commented

Getting Data from XML with LINQ

I have some problems with this code, everytime I start it it returns nothing.

Here's my MainWindow.xaml:

 using System;
 using System.Windows;
 using System.Windows.Controls;
 using WiederholÜbung.ServiceReference1;
    
 namespace WiederholÜbung
 {
     /// <summary>
     /// Interaction logic for MainWindow.xaml
     /// </summary>
     public partial class MainWindow : Window
     {
         private IWeatherService service;
         public MainWindow()
         {
             InitializeComponent();
             service = new WeatherServiceClient();
             DateTime now = DateTime.Now;
             datePicker.SelectedDate = now;
                
         }
         private void Button_Click(object sender, RoutedEventArgs e)
         {
         }
    
         private void datePicker_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
         {
    
         }
    
         private void Button_Click_1(object sender, RoutedEventArgs e)
         {
             Console.WriteLine("Button gedrückt");
             Console.WriteLine(datePicker.SelectedDate.Value.Date);
             WeatherForADay weather = service.GetWeatherData(datePicker.SelectedDate.Value.Date);
             Console.WriteLine(weather.date);
             tempLabel.Content = weather.temperatur;
         }
     }

And here's my IService:

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Runtime.Serialization;
 using System.ServiceModel;
 using System.Text;
    
 namespace WiederholServer
 {
     // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
     [ServiceContract]
     public interface IWeatherService
     {
    
         [OperationContract]
         WeatherForADay GetWeatherData(DateTime date);
    
     }
    
     [DataContract]
     public class WeatherForADay{
             [DataMember]
             public DateTime date { get; set; }
    
            [DataMember]
            public string temperatur { get; set; }
    
     }
 }

And my Service:

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Runtime.Serialization;
 using System.ServiceModel;
 using System.Text;
 using System.Xml.Linq;
    
 namespace WiederholServer
 {
     // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in both code and config file together.
     public class Service1 : IWeatherService
     {
         string weatherpath = @"C:\Users\yanni\source\repos\WiederholÜbung\WiederholServer/neueXML.xml";
         public XElement weather;
    
         public WeatherForADay GetWeatherData(DateTime date)
         {
             WeatherForADay weatherforaday = new WeatherForADay();
    
    
             if (date == null)
             {
                 throw new ArgumentNullException("date");
             }
             else
             {
                 weather = XElement.Load(weatherpath);
                 string temperatur = (from weather in weather.Elements("weather")
                                      where DateTime.Parse(weather.Element("date").Value).Date.Equals(date.Date)
                                      select weather.Element("temperature").Value).FirstOrDefault();
                 weatherforaday.date = date;
                 weatherforaday.temperatur = temperatur;
             }
    
             return weatherforaday;
         }
     }
 }





dotnet-csharp
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

  • What does the XML look like (suggest providing a small sample) and what is a test value for a date used for the where condition?

  • Are you simply compare date to date or datetime to datetime?


0 Votes 0 ·

0 Answers