C#: Adding elements to XML file

trevor1940
trevor1940 used Ask the Experts™
on
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

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
ste5anSenior Developer

Commented:
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.
Retired
Distinguished Expert 2017
Commented:
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

Author

Commented:
Thank You I can see now what I was doing wrong
Fernando SotoRetired
Distinguished Expert 2017

Commented:
Not a problem, glad I was able to help.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial