News Aggregator Sample
Explore a practical example of building a cross-platform news aggregator using .NET technologies.
Project Overview
This sample demonstrates how to create a modern, performant, and visually appealing news aggregator application that runs seamlessly on both iOS and Android devices using .NET MAUI (Multi-platform App UI). It showcases key features like:
- Fetching news articles from various RSS feeds.
- Parsing and displaying article content in a clean, readable format.
- Implementing a user-friendly interface with navigation and search capabilities.
- Handling data persistence for offline access (optional).
- Leveraging platform-specific features for an optimal user experience.
The application architecture follows best practices for mobile development, emphasizing separation of concerns and maintainable code.
Key Technologies Used
- .NET MAUI: For building cross-platform native UIs from a single codebase.
- C#: The primary programming language.
- XAML: For declarative UI definition.
- MVVM (Model-View-ViewModel): Architectural pattern for UI logic separation.
- HttpClient: For making network requests to fetch RSS feeds.
- XML Deserialization: For parsing RSS feed data.
- Optional: SQLite or other local storage: For caching data.
Code Snippets
Below are illustrative code snippets highlighting some core functionalities. For the full source code, please refer to the GitHub repository.
Fetching and Parsing RSS Feed
This C# code snippet shows how to use HttpClient to fetch an RSS feed and deserialize the XML content.
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
using System.Xml.Serialization;
public class RssItem
{
[XmlElement("title")]
public string Title { get; set; }
[XmlElement("link")]
public string Link { get; set; }
[XmlElement("description")]
public string Description { get; set; }
[XmlElement("pubDate")]
public string PubDate { get; set; }
}
[XmlRoot("channel")]
public class RssChannel
{
[XmlElement("item")]
public List<RssItem> Items { get; set; }
}
public class RssService
{
private readonly HttpClient _httpClient = new HttpClient();
public async Task<List<RssItem>> GetNewsAsync(string feedUrl)
{
try
{
var response = await _httpClient.GetStringAsync(feedUrl);
var serializer = new XmlSerializer(typeof(RssChannel));
using (var reader = new System.IO.StringReader(response))
{
var channel = (RssChannel)serializer.Deserialize(reader);
return channel.Items ?? new List<RssItem>();
}
}
catch (Exception ex)
{
Console.WriteLine($"Error fetching news: {ex.Message}");
return new List<RssItem>();
}
}
}
XAML for Article List Item
A simplified XAML snippet for displaying a single news item in a list view.
<ContentView xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="NewsAggregator.UI.Controls.RssListItemView"
HeightRequest="100" >
<Grid Padding="10" RowDefinitions="*,Auto,Auto" ColumnDefinitions="Auto,*">
<!-- Placeholder for Image (optional) -->
<Image Source="placeholder_image.png" WidthRequest="80" HeightRequest="80" Grid.RowSpan="3" Margin="0,0,10,0"/>
<Label Grid.Column="1" Grid.Row="0" Text="{Binding Title}" FontAttributes="Bold" LineBreakMode="TailTruncation"/>
<Label Grid.Column="1" Grid.Row="1" Text="{Binding Description}" MaxLines="2" LineBreakMode="TailTruncation" TextColor="{StaticResource LightTextColor}"/>
<Label Grid.Column="1" Grid.Row="2" Text="{Binding PubDate}" TextColor="{StaticResource LightTextColor}" FontSize="Micro"/>
</Grid>
</ContentView>
Getting Started
To run this sample, you will need:
- Visual Studio 2022 with the .NET MAUI workload installed.
- An iOS simulator or a connected iOS device, or an Android emulator or a connected Android device.
- Clone the repository from GitHub and open the solution in Visual Studio.
- Build and run the application on your chosen target platform.
Customize the RSS feeds, add new features, and adapt the UI to your specific needs.
This sample serves as a solid foundation for developing feature-rich mobile news applications with .NET.