Click here to Skip to main content
15,893,266 members
Home / Discussions / C#
   

C#

 
AnswerRe: Licence key Generate Pin
Pete O'Hanlon1-Jan-10 22:47
mvePete O'Hanlon1-Jan-10 22:47 
QuestionCreating trial version software Pin
Milind Panchal1-Jan-10 21:28
Milind Panchal1-Jan-10 21:28 
AnswerRe: Creating trial version software Pin
venomation1-Jan-10 22:22
venomation1-Jan-10 22:22 
GeneralRe: Creating trial version software Pin
Dave Kreskowiak2-Jan-10 9:30
mveDave Kreskowiak2-Jan-10 9:30 
GeneralRe: Creating trial version software Pin
venomation2-Jan-10 12:25
venomation2-Jan-10 12:25 
AnswerRe: Creating trial version software Pin
Isaac Gordon1-Jan-10 22:37
Isaac Gordon1-Jan-10 22:37 
QuestionCombobox in C# Pin
Anu_Bala1-Jan-10 17:59
Anu_Bala1-Jan-10 17:59 
QuestionAny ideas on making this code better? Pin
venomation1-Jan-10 15:50
venomation1-Jan-10 15:50 
I have made this small application that can write and read XML and I am new to XML parsing.
The following source is probably pretty bad so does anyone have some ideas to make it better for example, a better design strategy for it?

I would really like to follow a good conduct before I look further Big Grin | :-D

The main class (normal console application)
using System;
using System.Collections.Generic;
using System.Text;
namespace xml
{
    class Program
    {

        static void Main(string[] args)
        {
            List<Product> list = new List<Product>() 
            { 
                new Product(0, "Xbox 360", ProductType.Electronic, 129.99m),
                new Product(1,"Playstation 3",ProductType.Electronic,130.99m),
                new Product(2,"Playmobile Tonka",ProductType.Toy,29.99m),
                new Product(3,"Tennis rack",ProductType.Sports,50.99m)
            };

            XmlRules rules = new XmlRules();
            rules.SortList(ref list, SortType.IDLowest);
            Xml xml = new Xml();
            xml.SaveProductAsXML(list);
            xml.ReadXml();

            list = null;

        }

    }

    public class Product
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public ProductType Type { get; set; }
        public decimal Cost { get; set; }

        public Product(int id, string name, ProductType type, decimal cost)
        {
            ID = id;
            Name = name;
            Type = type;
            Cost = cost;
        }


    }
    public enum ProductType
    {
        Electronic, Toy, Sports
    }
}


An XML class that deals with saving and reading XML:
using System;
using System.IO;
using System.Collections.Generic;
using System.Xml;

namespace xml
{
    class Xml
    {
        const string _FILE_NAME = "products.xml";
        public void SaveProductAsXML(List<Product> list)
        {
            XmlDocument root = new XmlDocument();
            XmlElement rootElement = root.CreateElement("Products");
            root.AppendChild(rootElement);

            for (int i = 0; i < list.Count; i++)
            {
                XmlElement elemnt = root.CreateElement("Product");
                XmlAttribute att = root.CreateAttribute("Name");
                att.Value = list[i].Name;

                elemnt.Attributes.Append(att);

                XmlAttribute idAttr = root.CreateAttribute("ID");

                idAttr.Value = list[i].ID.ToString();

                elemnt.Attributes.Append(idAttr);

                XmlAttribute typeAttr = root.CreateAttribute("Type");
                typeAttr.Value = list[i].Type.ToString();

                elemnt.Attributes.Append(typeAttr);

                XmlAttribute priceAttr = root.CreateAttribute("Price");
                priceAttr.Value = string.Format("{0}", list[i].Cost);

                elemnt.Attributes.Append(priceAttr);

                rootElement.AppendChild(elemnt);
            }

            root.Save(_FILE_NAME);
            root = null;
            rootElement = null;
        }

        public void ReadXml()
        {
            using (FileStream file = File.OpenRead(_FILE_NAME))
            {
                XmlDocument document = new XmlDocument();
                document.Load(file);

                XmlNodeList nodeList = document.GetElementsByTagName("Product");

                for (int i = 0; i < nodeList.Count; i++)
                {
                    string name = nodeList[i].Attributes[0].InnerText;
                    string id = nodeList[i].Attributes[1].InnerText;
                    string type = nodeList[i].Attributes[2].InnerText;
                    string price = string.Format("{0:c}", decimal.Parse(nodeList[i].Attributes[3].InnerText));

                    Console.WriteLine("Name {0}", name);
                    Console.WriteLine("ID {0}", id);
                    Console.WriteLine("Type {0}", type);
                    Console.WriteLine("Price {0}", price);
                    Console.WriteLine();
                }

            }


            Console.ReadKey();
        }
    }
}


And finally a class that stores the sorting rules for the XML document:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace xml
{
    class XmlRules
    {
        public void SortList(ref List<Product> list, SortType sort)
        {
            IEnumerable<Product> products;

            if (sort == SortType.PriceLowest || sort == SortType.PriceHighest)
            {
                products =
                    from p in list
                    orderby (sort == SortType.PriceHighest ? p.Cost * -1 : p.Cost)
                    select p;
                list = products.ToList();
            }
            else if (sort == SortType.IDLowest || sort == SortType.IDHighest)
            {
                products =
                    from p in list
                    orderby (sort == SortType.IDHighest ? p.ID * -1 : p.ID)
                    select p;
                list = products.ToList();
            }


            products = null;

        }

    }
    public enum SortType
    {
        PriceLowest, PriceHighest, IDLowest, IDHighest
    }
}


Thanks!
AnswerRe: Any ideas on making this code better? Pin
#realJSOP2-Jan-10 3:00
mve#realJSOP2-Jan-10 3:00 
QuestionNewbie Question Pin
kruegersck1-Jan-10 12:23
kruegersck1-Jan-10 12:23 
AnswerRe: Newbie Question Pin
Eric Dahlvang1-Jan-10 13:18
Eric Dahlvang1-Jan-10 13:18 
AnswerRe: Newbie Question Pin
Abhinav S1-Jan-10 16:58
Abhinav S1-Jan-10 16:58 
AnswerRe: Newbie Question Pin
Isaac Gordon1-Jan-10 22:48
Isaac Gordon1-Jan-10 22:48 
QuestionState signalling from windows services Pin
minnie mouse1-Jan-10 11:43
minnie mouse1-Jan-10 11:43 
AnswerRe: State signalling from windows services Pin
Jimmanuel1-Jan-10 12:25
Jimmanuel1-Jan-10 12:25 
GeneralRe: State signalling from windows services Pin
minnie mouse2-Jan-10 8:54
minnie mouse2-Jan-10 8:54 
QuestionHow to increase concurrent parallel tasks with System.Threading.Parallel (.Net 4.0) Pin
Gilad Kapelushnik1-Jan-10 11:19
Gilad Kapelushnik1-Jan-10 11:19 
GeneralRe: How to increase concurrent parallel tasks with System.Threading.Parallel (.Net 4.0) [modified] Pin
harold aptroot1-Jan-10 11:24
harold aptroot1-Jan-10 11:24 
GeneralRe: How to increase concurrent parallel tasks with System.Threading.Parallel (.Net 4.0) Pin
Gilad Kapelushnik1-Jan-10 11:51
Gilad Kapelushnik1-Jan-10 11:51 
GeneralRe: How to increase concurrent parallel tasks with System.Threading.Parallel (.Net 4.0) Pin
harold aptroot1-Jan-10 12:19
harold aptroot1-Jan-10 12:19 
AnswerRe: How to increase concurrent parallel tasks with System.Threading.Parallel (.Net 4.0) Pin
Nicholas Butler1-Jan-10 13:26
sitebuilderNicholas Butler1-Jan-10 13:26 
AnswerRe: How to increase concurrent parallel tasks with System.Threading.Parallel (.Net 4.0) Pin
Eric Dahlvang1-Jan-10 14:26
Eric Dahlvang1-Jan-10 14:26 
Questiontelnet server in c# Pin
rjs1 41-Jan-10 9:18
rjs1 41-Jan-10 9:18 
AnswerRe: telnet server in c# [modified] Pin
Abhijit Jana1-Jan-10 9:42
professionalAbhijit Jana1-Jan-10 9:42 
GeneralRe: telnet server in c# Pin
Abhishek Sur1-Jan-10 9:58
professionalAbhishek Sur1-Jan-10 9:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.