C#: Adding elements to XML file

Using XML Linq  how do I add a new  TV show to the attached XML TV.xml preferable so the show.names are in alphabetical order?

From MyForm I call Thus

xmlFuncs.AddShowName(ShowName, FolderName, TMDBid, Xdoc);

Open in new window

or

xmlFuncs.AddShowName("Charmed", "Charmed 2018" "79611", Xdoc);

Open in new window


this creates
NewShow = {<Show>
  <Name>Charmed</Name>
  <FolderName>Charmed 2018</FolderName>
  <TMDBid>79611</TMDBid>
</Show>}

Open in new window




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
}

Open in new window

LVL 1
trevor1940Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ste5anSenior DeveloperCommented:
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.
Fernando SotoRetiredCommented:
Hi trevor1940;
 
I modified your code to work first adding the new show and then sorting the shows. You were adding the new show to the results of the query and not the Xdoc itself.
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();


    if (show == null)
    {
        // Get a reference to the Root element and add it to it.
        Xdoc.Root.Add(NewShow);
        // 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");
    }

}

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
trevor1940Author Commented:
Thank You I can see now what I was doing wrong
Fernando SotoRetiredCommented:
Not a problem, glad I was able to help.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.