troubleshooting Question

C#: Adding elements to XML file part2

Avatar of trevor1940
trevor1940 asked on
XML.NET ProgrammingC#
15 Comments1 Solution282 ViewsLast Modified:
Hi this is a follow on from here  and other questions relating to parsing XML with C#

I'm trying to add a new season to the xml

My CheckSeason fails where no season exists eg for "A Discovery Of Witches"  but returns true or false for any MacGyver season

I'm assuming it's because the parent "season" has to exist before you can check for season number?
I don't understand why this causes an "unhandled exception " and not just return null


AddSeason i've tried to amend from previous question  but i'm missing something

Logic
Add season if none exist
Add new season in numerical  SeasonNumber order


       public static XDocument xdoc = XDocument.Load(@"J:\Media\TV\TV.xml");
        // Load the XML Document using Linq to XML


            if (CheckSeason("A Discovery Of Witches", "S01", "77236"))  // fails
            //    if (CheckSeason("MacGyver 2016", "S02", "67133"))        // works
                {
                Console.WriteLine("Exists");
            }
            else
            {
                Console.WriteLine("Does Not Exists");
                Console.WriteLine("Add Season");
                AddSeason("A Discovery Of Witches", "S01", "77236");
            }


        private static void AddSeason(string showname, string season, string TMDBid)
        {
            char[] array1 = { 's', 'S', '0' };
            foreach (char c in array1)
            {
                season = season.TrimStart(c);
            }
            var NewSeason = new XElement("Season",
               new XElement("SeasonNumber", season));

            var showSeason = (from node in xdoc.Descendants("Show")
                        where node.Element("TMDBid").Value == TMDBid
                        select node).FirstOrDefault();


            if (showSeason == null)
            {
                // Get a reference to the Show element and add it to it.
                showSeason.AddAfterSelf(NewSeason);
                // Sort the Shows in the document
                //var showOrdered = (from shows in xdoc.Descendants("Show")
                //                   orderby shows.Element("Name").Value
                //                   select shows).ToList();

                //// Remove the unsorted shows
                //xdoc.Root.RemoveAll();
                //// Add the sorted shows
                //xdoc.Root.Add(showOrdered);

                // Save the document with the new Show
                xdoc.Save(@"J:\Media\TV\TVNew.xml");
            }



        }

        private static bool CheckSeason(string showname, string season, string TMDBid)
        {
            char[] array1 = { 's', 'S', '0' };
            foreach (char c in array1)
            {
                season = season.TrimStart(c);
            }
            // // Checks for the existence of a Show Season
            var results = (from node in xdoc.Descendants("Show")
                           where node.Element("TMDBid").Value == TMDBid &&
                                 node.Element("Season").Element("SeasonNumber").Value == season
                           select node).FirstOrDefault();
            return results != null ? true : false;
        }

TV.xml

<?xml version="1.0" encoding="utf-8"?>
<TV>
  <Show>
    <Name>A Discovery Of Witches</Name>
    <FolderName>A Discovery Of Witches</FolderName>
    <TMDBid>77236</TMDBid>
  </Show>
  <Show>
    <Name>MacGyver 2016</Name>
    <FolderName>MacGyver 2016</FolderName>
    <TMDBid>67133</TMDBid>
    <Season>
      <SeasonNumber>3</SeasonNumber>
      <Episode>
        <EpisodeNumber>1</EpisodeNumber>
        <EpisodeName>Improvise</EpisodeName>
        <BackDrop>https://image.tmdb.org/t/p/w350_and_h196_bestv2/eQMVJkBltvID73sh9vvTistssS6.jpg</BackDrop>
        <FullPath>J:\Media\TV\MacGyver (2016)\S03\Macgyver.2016.S03E01.Improvise.mp4</FullPath>
      </Episode>
    </Season>
  </Show>
</TV>
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 15 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 15 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros