Avatar of trevor1940
trevor1940
 asked on

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

.NET ProgrammingC#XML

Avatar of undefined
Last Comment
Fernando Soto

8/22/2022 - Mon
ste5an

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.
ASKER CERTIFIED SOLUTION
Fernando Soto

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
trevor1940

ASKER
Thank You I can see now what I was doing wrong
Fernando Soto

Not a problem, glad I was able to help.
Your help has saved me hundreds of hours of internet surfing.
fblack61