locked
replace simple xml generation with switch & loop with enum RRS feed

  • Question

  • User-240513752 posted
    public static void Main()
            {
                XNamespace ns = "http://www.sitemaps.org/schemas/sitemap/0.9";
                XElement myXml = new XElement(ns + "urlset",
                                              new XElement(ns + "url", //Home
                                                           new XElement(ns + "loc", "http://www.example.com/"),
                                                           new XElement(ns + "lastmod", "2012-04-30"),
                                                           new XElement(ns + "changefreq", "monthly"),
                                                           new XElement(ns + "priority", "1.0")),
                                              new XElement(ns + "url",//aboutus
                                                           new XElement(ns + "loc", "http://www.example.com/about-us.html"),
                                                           new XElement(ns + "changefreq", "yearly"),               
    new XElement(ns + "priority", "0.4")));

                                              new XElement(ns + "url",//Process
                                                           new XElement(ns + "loc", "http://www.example.com/Process.html"),
                                                           new XElement(ns + "changefreq", "daily"),
                                                           new XElement(ns + "priority", "0.4")));

    Console.WriteLine(myXml.ToString()); myXml.Save("tesqt-doc.xml");

    I have this method ,

    but i want to use a switch with loop and then generate the XML file. like this

         static void Main(string[] args)
            {
                XMLFile();
            }
    
            private static void XMLFile()
            {
                foreach (Foos foo in Enum.GetValues(typeof(Foos)))
                {
                    switch (foo)
                    {
                        case Foos.Home:
                            Console.WriteLine("The volume has been turned Down.");
                            break;
                        case Foos.AboutUs:
                            Console.WriteLine("The volume is in the middle.");
                            break;
                        case Foos.Process:
                            Console.WriteLine("The volume has been turned up.");
                            break;
                    }
                }
    
            }
            public enum Foos
            {
                Home,
                AboutUs,
                Process
            }

    How can i replace that with this one?

    FYI* Here are 3 options for this moment. but it can be more as requirement which i can't copy here..

    Monday, October 24, 2016 7:36 PM

Answers

  • User-271186128 posted

    Hi khan_1,

    but i want to use a switch with loop and then generate the XML file. like this

    Based on your requirement, you could modify your code as below, you could use XElement.Add method to add element:

           static void Main(string[] args)
            {
                XMLFile();
            }
    
            private static void XMLFile()
            {
                XNamespace ns = "http://www.sitemaps.org/schemas/sitemap/0.9";
                XElement myXml = new XElement(ns + "urlset");
                foreach (Foos foo in Enum.GetValues(typeof(Foos)))
                {
                    switch (foo)
                    {
                        case Foos.Home:
                            Console.WriteLine("The volume has been turned Down.");
                            myXml.Add(new XElement(ns + "url", //Home
                                                           new XElement(ns + "loc", "http://www.example.com/"),
                                                           new XElement(ns + "lastmod", "2012-04-30"),
                                                           new XElement(ns + "changefreq", "monthly"),
                                                           new XElement(ns + "priority", "1.0")));
                            break;
                        case Foos.AboutUs:
                            Console.WriteLine("The volume is in the middle.");
                            myXml.Add(new XElement(ns + "url",//aboutus
                                                           new XElement(ns + "loc", "http://www.example.com/about-us.html"),
                                                           new XElement(ns + "changefreq", "yearly"),
                                                           new XElement(ns + "priority", "0.4")));
                            break;
                        case Foos.Process:
                            Console.WriteLine("The volume has been turned up.");
                            myXml.Add(new XElement(ns + "url",//Process
                                                             new XElement(ns + "loc", "http://www.example.com/Process.html"),
                                                             new XElement(ns + "changefreq", "daily"),
                                                             new XElement(ns + "priority", "0.4")));
                            break;
                    }
                }
                Console.WriteLine(myXml);
                myXml.Save("tesqt-doc.xml");
            }
            public enum Foos
            {
                Home,
                AboutUs,
                Process
            }

    Best regards,
    Dillion

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 26, 2016 5:49 AM

All replies

  • User303363814 posted

    Replace the Console.WriteLine statements with statements that generate the xml that you want. 

    (BTW I assume you realise that your loop+switch code is a very verbose way to write three lines of code.  The entire body of XMLFile() (all 15 lines of it) could be replaced with just the three Console.WriteLine statements, in the order that you want them.  Easier to read, easier to write, easier to understand.  Are you paid per line of code Smile?)

    Monday, October 24, 2016 10:18 PM
  • User-271186128 posted

    Hi khan_1,

    but i want to use a switch with loop and then generate the XML file. like this

    Based on your requirement, you could modify your code as below, you could use XElement.Add method to add element:

           static void Main(string[] args)
            {
                XMLFile();
            }
    
            private static void XMLFile()
            {
                XNamespace ns = "http://www.sitemaps.org/schemas/sitemap/0.9";
                XElement myXml = new XElement(ns + "urlset");
                foreach (Foos foo in Enum.GetValues(typeof(Foos)))
                {
                    switch (foo)
                    {
                        case Foos.Home:
                            Console.WriteLine("The volume has been turned Down.");
                            myXml.Add(new XElement(ns + "url", //Home
                                                           new XElement(ns + "loc", "http://www.example.com/"),
                                                           new XElement(ns + "lastmod", "2012-04-30"),
                                                           new XElement(ns + "changefreq", "monthly"),
                                                           new XElement(ns + "priority", "1.0")));
                            break;
                        case Foos.AboutUs:
                            Console.WriteLine("The volume is in the middle.");
                            myXml.Add(new XElement(ns + "url",//aboutus
                                                           new XElement(ns + "loc", "http://www.example.com/about-us.html"),
                                                           new XElement(ns + "changefreq", "yearly"),
                                                           new XElement(ns + "priority", "0.4")));
                            break;
                        case Foos.Process:
                            Console.WriteLine("The volume has been turned up.");
                            myXml.Add(new XElement(ns + "url",//Process
                                                             new XElement(ns + "loc", "http://www.example.com/Process.html"),
                                                             new XElement(ns + "changefreq", "daily"),
                                                             new XElement(ns + "priority", "0.4")));
                            break;
                    }
                }
                Console.WriteLine(myXml);
                myXml.Save("tesqt-doc.xml");
            }
            public enum Foos
            {
                Home,
                AboutUs,
                Process
            }

    Best regards,
    Dillion

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 26, 2016 5:49 AM