troubleshooting Question

C#: Adding elements to XML file

Avatar of trevor1940
trevor1940 asked on
XML.NET ProgrammingC#
4 Comments1 Solution261 ViewsLast Modified:
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);
or

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

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



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
}
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 4 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 4 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