I am currently making a simple tool that allows you to make different types of searches at once. So far I have done this by implementing a Site class, creating a list of sites I would like to search, then opening a new window with a Browser element with the search URL that I need for each active site.
There is also a method that creates a new site and adds it to the list.
Site.cs
public class Site
{
public int type { get; set; }
public string URL { get; set; }
public string extras { get; set; }
public string name { get; set; }
public bool IsChecked { get; set; }
public Uri GetMySearch(string query)
{
switch (type)
{
case 0: //Simple Search
return new Uri(URL + query);
case 1: //API call
return new Uri(URL + query + extras);
default: return null;
}
}
}
some parts of Mainwindow.Cs
private void Window_Loaded(object sender, RoutedEventArgs e) //initialization
{
Site Google = new Site();
Google.URL = "https://google.com/search?q=";
Google.type = 0;
Google.name = "google";
Site Wolfram = new Site();
Wolfram.URL = "https://api.wolframalpha.com/v1/simple?i=";
Wolfram.type = 1;
Wolfram.extras = "&appid=2GA4A5-YL7HY9KR42";
Wolfram.name = "Wolfram";
Site Wikipedia = new Site();
Wikipedia.URL = "https://google.com/search?q=site:wikipedia.org";
Wikipedia.type = 0;
Wikipedia.name = "Wikipedia";
sites.Add(Google);
sites.Add(Wolfram);
sites.Add(Wikipedia);
SitesDisplay.ItemsSource = sites;
}
private void AddNewSiteButton(object sender, RoutedEventArgs e)
{
SiteEntryWindow siteEntryWindow = new SiteEntryWindow("Enter the display name of your site \nOr leave blank to default to URL");
if (siteEntryWindow.ShowDialog() == true)
{
if (siteEntryWindow.url.Length > 3)
AddASite(siteEntryWindow.url, siteEntryWindow.MyName);
else MessageBox.Show("Please enter a valid url!", "Oops!", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
public void AddASite(string url, string name = "")
{
Site mySite = new Site();
if (name.Length > 0)
mySite.name = name;
else mySite.name = url;
mySite.URL = "https://google.com/search?q=site:" + url;
mySite.type = 0;
sites.Add(mySite);
SitesDisplay.ItemsSource = null;
SitesDisplay.ItemsSource = sites;
}
Essentially I am trying to figure out how I can save the users list of sites to a txt file (or whatever would be more applicable in this case) and then load it when the application opens. I tried using StreamWriter and WriteAllLines but since it is a list of sites they do not know what to do.
I could probably use lots of if else to process each individual class attribute into strings and then write it to a txt file but I would have to parse it whenever I would want to load and I cannot imagine that would be easy.
This person seemed to be having a similar issue but I'm not sure if an XML file is the best solution and XmlSerializer seemed to be beyond me. Is there a simpler / better way to do this?
I am still somewhat new to C# and WPF so sorry if this is a silly question or has a easy solution.