using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.IO;using System.Xml.Linq;namespace TVDB{ class XMLFuncs { public XDocument Xdoc; public XDocument LoadXML(string XMLpath) { if (File.Exists(XMLpath)) { Xdoc = XDocument.Load(XMLpath); return Xdoc; } else { return null; } } public string ChecKShowName(string FolderName, XDocument Xdoc) { // // Checks for the existence of a Show and return it's TMDB ID var results = (from node in Xdoc.Descendants("Show") where node.Element("FolderName").Value == FolderName select node.Element("TMDBid").Value).FirstOrDefault(); if (results != null) { return results; } else { return null; } } internal void AddShowName(string Name, string FolderName, string TMDBid, XDocument Xdoc) { var NewShow = new XElement("Show", new XElement("Name", Name), new XElement("FolderName", FolderName), new XElement("TMDBid", TMDBid) ); var show = (from node in Xdoc.Descendants("Show") where node.Element("FolderName").Value == FolderName select node).FirstOrDefault();// form crashes after here with no errors if (show == null) { show.Add(NewShow); // Save the document with the new Show Xdoc.Save(@"J:\Media\TV\TVNew.xml"); } } }// end class XMLFuncs}
Abstract your problem. Instead of thinking in procedures and functions on XML files, you should use an database/repository type to decouple the actual storage (XML) from the rest of you application.