Getting Data from XML with LINQ

Mantra 1 Reputation point
2021-06-27T19:21:28.093+00:00

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;  
        }  
    }  
}  




 
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,418 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.